<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>
搜索框的实现JS
最新推荐文章于 2024-06-28 13:39:22 发布