您的位置:首页 > Web前端 > JavaScript

搜索框的实现JS

2008-06-26 12:13 134 查看
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: