Effect:


:
the document. The writeln (" <div style=" position: relative; margin - left: 100px; margin - top: 100px; \ ">");
document.write("var selectedIndex=0;
var showSuggest=function(inputObj){
var xmlHttp=$ajax();
var key=document.getElementById("tbxsearch").value;
if(key==lastKey)return false;
else lastkey=key;
if(/^\s*$/.test(key)){
suggestDiv.style.display="none";
return false;
};
selectedIndex=-1;
xmlHttp.open("get","js/search.aspx?skey="+encodeURI(key)+"&t=" + new Date().getTime(),true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
var xmldoc=xmlHttp.responseXML;
keynodes=xmldoc.getElementsByTagName("key");

if(keynodes.length>0)
{
suggestDiv.innerHTML="";
for(var i=0;ivar keyDiv=$ctag("div");
keyDiv.style.width="100%";
keyDiv.style.padding="0";
keyDiv.style.margin="2px 0 2px 0";
keyDiv.style.height="18px";
keyDiv.style.lineHeight="18px";
keyDiv.style.cursor="default";
keyDiv.style.textIndent="4px";

keyDiv.innerHTML=keynodes[i].getAttribute("value");

keyDiv.onmouseover=function(){
selectedIndex=-1;
divlist=suggestDiv.getElementsByTagName("div");
for(var k=0;kdivlist[k].className="";
if(divlist[k]==this)selectedIndex=k;
}
this.className="suggestOver";
};

keyDiv.onmouseout=function(){
this.className="";
};

keyDiv.onmousedown=function(){
document.getElementById("tbxsearch").value=http://www.script-home.com//article/this.innerHTML;
//这里写执行搜索的代码
};

suggestDiv.appendChild(keyDiv);
};

suggestDiv.style.display="block";
}else{
suggestDiv.style.display="none";
}
}
};
xmlHttp.send(null);
};
var changeSelect=function(isUp,inputObj){
if (isUp) selectedIndex++;
else selectedIndex--;
if(selectedIndex<0) selectedIndex=0;
var divlist=suggestDiv.getElementsByTagName("div");
if(selectedIndex>=divlist.length)
selectedIndex=divlist.length-1;
for(var i=0;iif(i==selectedIndex){
divlist[i].className="suggestOver";
inputObj.value=http://www.script-home.com//article/divlist[i].innerHTML.replace(/<[^>]+>/g,"");
}else{
divlist[i].className="";
}
}
};
var suggest=function(event,inputObj){
event=event ? event : (window.event ? window.event : null);
var keyCode=event.keyCode;

if(keyCode==13){
if(selectedIndex !=-1){
inputObj.value=http://www.script-home.com//article/keynodes[selectedIndex].getAttribute("value");
}
//这里写执行搜索
}

if(keyCode==40||keyCode==38){
var isUp=false;
if(keyCode==40) {isUp=true;}
changeSelect(isUp,inputObj);
}else{
showSuggest(inputObj);
}


};
var inputOnblur=function(){setTimeout('suggestDiv.style.display="none";',100);};

search.aspx代码:

<%@ Page Language="C#" %>


打包下载http://xiazai.script-home.com/200904/yuanma/ajaxsuggest.rar


This concludes the body part