搜索框的实现JS

来源:互联网 发布:怎么网络监听 编辑:程序博客网 时间:2024/06/03 22:40
 <script language="javascript" type="text/javascript">        if(typeof String.__Trimed =="undefined"){           String.prototype.trim = function()                 {                      var t = this.replace(/(^/s*)|(/s*$)/g, "");                    return t.replace(/(^ *)|( *$)/g, "");               }           String.__Trimed=true ;     }     function ajaxSeacher(keyTextId,actButtionId,url){                 this.keyContainer=keyTextId ;         this.keyButton=actButtionId ;         this.url=url ;         var cTh=this ;         this.init=function(){             var o=document.getElementById(cTh.keyContainer );             var t=document.getElementById(cTh.keyButton );             if(o.attachEvent){               o.attachEvent("onkeydown",cTh.nameKeyDown);               t.attachEvent("onclick",cTh.bClick );             } else {                 o.addEventListener("keydown",cTh.nameKeyDown);                t.addEventListener("click",cTh.bClick,false );             }                      }               this.nameKeyDown=function (evt){               if(window.event){                       if(event.keyCode==13 ){                           document.getElementById(cTh.keyButton ).click();                           event.returnValue=false ;                       }               } else {                       if(evt.keyCode==13){                           document.getElementById(cTh.keyButton ).click();                           evt.returnValue=false ;                       }               }                       }                  this.validate=function(){             var o=document.getElementById(cTh.keyContainer );             var v=o.value;             if(v.trim()==""){                alert("搜索的关键字为空!");                return false ;             }             return true ;         }         this.bClick=function(evt){                if(cTh.validate ()==true ){                   window.location.href=url+"?key="+escape(document.getElementById(cTh.keyContainer ).value);                    }                  }                       }     </script>   <div id="ajaxSearch">        <div><a id="ajsNews">新闻</a><a id="ajsCourse">课程</a><a id="ajsKb">知识</a></div>        <input type="text" name="schKey" id="ajaxSechkey" /><input type="button" id="sckBtn"  value ="搜索" />    </div>   <script language="javascript" type="text/javascript">      var aj=new ajaxSeacher("ajaxSechkey","sckBtn","ajaxTree2.htm");      aj.init();   </script>
原创粉丝点击