日期:2014-05-16 浏览次数:20659 次
<%@page contentType="text/html;charset=gbk"%> <%@page import ="org.dom4j.*"%> <head> <title>测试代码</title> <!--这里是Ajax的地方--> <script type="text/javascript"> var xmlHttp; var completeDiv; var inputField; var nameTable; var nameTableBody; var inputField; function createXMLHttpRequest(){//创建xmlhttp xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } function initVars(){//初始化成员变量 inputField=document.getElementById("names"); nameTable=document.getElementById("name_table"); completeDiv=document.getElementById("popup"); nameTableBody=document.getElementById("name_table_body"); } function findNames(){//发送请求 initVars(); if(inputField.value.length>0){ createXMLHttpRequest(); var url="test12.jsp?names="+escape(inputField.value); xmlHttp.open("get",url,true); xmlHttp.onreadystatechange=callback;xmlHttp.send(null); } else{ clearNames(); } } function clearNames(){//清除信息 var ind=nameTableBody.childNodes.length; for(var i=ind-1;i>=0;i--){ nameTableBody.removeChild(nameTableBody.childNodes[i]); } completeDiv.style.border="none"; } function callback(){ if(xmlHttp.readyState==4) if(xmlHttp.status==200){ var name=xmlHttp.responseXML.getElementsByTagName("name"); //var name=xmlHttp.responseText; alert(name); setNames(name); }else if(xmlHttp.status==204){ clearNames(); } } function setNames(the_names) { clearNames(); var size=the_names.length; setOffsets(); var row,cell,txtNode; for(var i=0;i<size&&i<10;i++){//如果超过十条记录只显示十条 var nextNode=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); } alert(nameTableBody); } function setOffsets(){ var end=inputField.offsetWidth; var left=calculateOffsetLeft(inputField); var top=calculateOffsetTop(inputField)+inputField.offsetHeight; completeDiv.style.border="black 1px solid"; completeDiv.style.left=