<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<title>AjaxAutoComplete</title>

<styletype="text/css">

.mouseOut{

background:#708090;

color:#FFFAFA;

}

.mouseOver{

background:#FFFAFA;

color:#000000;

}

</style>

<scripttype="text/javascript">

varxmlHttp;

varcompleteDiv;

varinputField;

varnameTable;

varnameTableBody;

functioncreateXMLHttpRequest(){

if(window.ActiveXObject){

xmlHttp=newActiveXObject("Microsoft.XMLHTTP");

}

elseif(window.XMLHttpRequest){

xmlHttp=newXMLHttpRequest();

}

}

functioninitVars(){

inputField=document.getElementById("names");

nameTable=document.getElementById("name_table");

completeDiv=document.getElementById("popup");

nameTableBody=document.getElementById("name_table_body");

}

functionfindNames(){

initVars();

if(inputField.value.length>0){

createXMLHttpRequest();

varurl="AutoCompleteServlet?names="+escape(inputField.value);

xmlHttp.open("GET",url,true);

xmlHttp.onreadystatechange=callback;

xmlHttp.send(null);

}else{

clearNames();

}

}

functioncallback(){

if(xmlHttp.readyState==4){

if(xmlHttp.status==200){

varname=xmlHttp.responseXML.getElementsByTagName("name")[0].firstChild.data;

setNames(xmlHttp.responseXML.getElementsByTagName("name"));

}elseif(xmlHttp.status==204){

clearNames();

}

}

}

functionsetNames(the_names){

clearNames();

varsize=the_names.length;

setOffsets();

varrow,cell,txtNode;

for(vari=0;i<size;i++){

varnextNode=the_names[i].firstChild.data;

row=document.createElement("tr");

cell=document.createElement("td");

cell.onmouseout=function(){this.className='mouseOver';};

cell.onmouseover=function(){this.className='mouseOut';};

cell.setAttribute("bgcolor","#FFFAFA");

cell.setAttribute("border","0");

cell.onclick=function(){populateName(this);};

txtNode=document.createTextNode(nextNode);

cell.appendChild(txtNode);

row.appendChild(cell);

nameTableBody.appendChild(row);

}

}

functionsetOffsets(){

varend=inputField.offsetWidth;

varleft=calculateOffsetLeft(inputField);

vartop=calculateOffsetTop(inputField)+inputField.offsetHeight;

completeDiv.style.border="black1pxsolid";

completeDiv.style.left=left+"px";

completeDiv.style.top=top+"px";

nameTable.style.width=end+"px";

}

functioncalculateOffsetLeft(field){

returncalculateOffset(field,"offsetLeft");

}

functioncalculateOffsetTop(field){

returncalculateOffset(field,"offsetTop");

}

functioncalculateOffset(field,attr){

varoffset=0;

while(field){

offset+=field[attr];

field=field.offsetParent;

}

returnoffset;

}

functionpopulateName(cell){

inputField.value=cell.firstChild.nodeValue;

clearNames();

}

functionclearNames(){

varind=nameTableBody.childNodes.length;

for(vari=ind-1;i>=0;i--){

nameTableBody.removeChild(nameTableBody.childNodes[i]);

}

completeDiv.style.border="none";

}

</script>

</head>

<body>

<h1>谷歌定位</h1>

搜索<inputtype="text"size="20"id="names"onkeyup="findNames();"style="height:20;"/>

<divstyle="position:absolute;"id="popup">

<tableid="name_table"bgcolor="#FFFAFA"border="0"cellspacing="0"cellpadding="0"/>

<tbodyid="name_table_body"></tbody>

</table>

</div>

</body>

</html>

实现思路:利用键盘输入的完结响应时间,从后来获取数据,然后解析数据进行显示,其中有很多需要注意的小细节的地方。本例子来源于AJAX基础教程书籍