搜索框的实现JS

 <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>

posted on 2008-06-26 12:13  老代哥哥  阅读(519)  评论(0编辑  收藏  举报

导航