在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
源代码: 脚本一: <!DOCTYPE html> <html> <head> <title>Auto-fill Form Fields</title> <link rel="stylesheet"href="script06.css" rel="external nofollow" > <script src="script06.js"></script> </head> <body> <form action="#"> Please enter your state:<br> <input type="text" id="searchField" autocomplete="off"><br> <div id="popups"> </div> </form> </body> </html> 脚本二: body, #searchfield { font: 1.2em arial, helvetica,sans-serif; } .suggestions { background-color: #FFF; padding: 2px 6px; border: 1px solid #000; } .suggestions:hover { background-color: #69F; } #popups { position: absolute; } #searchField.error { background-color: #FFC; } 脚本三: window.onload = initAll; var xhr = false; var statesArray = new Array(); function initAll() { document.getElementById("searchField").onkeyup = searchSuggest; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { if (window.ActiveXObject) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } if (xhr) { xhr.onreadystatechange = setStatesArray; xhr.open("GET", "us-states.xml",true); xhr.send(null); } else { alert("Sorry, but I couldn't create an XMLHttpRequest"); } } function setStatesArray() { if (xhr.readyState == 4) { if (xhr.status == 200) { if (xhr.responseXML) { var allStates = xhr.responseXML.getElementsByTagName("item"); for (var i=0; i<allStates.length; i++) { statesArray[i] = allStates[i].getElementsByTagName("label")[0].firstChild; } } } else { alert("There was a problem with the request " + xhr.status); } } } function searchSuggest() { var str = document.getElementById("searchField").value; document.getElementById("searchField").className = ""; if (str != "") { document.getElementById("popups").innerHTML = ""; for (var i=0; i<statesArray.length;i++) { var thisState = statesArray[i].nodeValue; if (thisState.toLowerCase().indexOf(str.toLowerCase())== 0) { var tempDiv = document.createElement("div"); tempDiv.innerHTML = thisState; tempDiv.onclick = makeChoice; tempDiv.className = "suggestions"; document.getElementById("popups").appendChild(tempDiv); } } var foundCt = document.getElementById("popups").childNodes.length; if (foundCt == 0) { document.getElementById("searchField").className ="error"; } if (foundCt == 1) { document.getElementById("searchField").value = document.getElementById("popups"). firstChild.innerHTML; document.getElementById("popups").innerHTML = ""; } } } function makeChoice(evt) { if (evt) { var thisDiv = evt.target; } else { var thisDiv = window.event.srcElement; } document.getElementById("searchField").value = thisDiv.innerHTML; document.getElementById("popups").innerHTML = ""; } 分析如下: 1. Please enter your state:<br> 4. if (xhr.responseXML) { var allStates = xhr.responseXML.getElementsByTagName("item"); for (var i=0; i<allStates.length; i++) { statesArray[i] = allStates[i].getElementsByTagName("label")[0].firstChild; } } 我们在这里读取文件,查看每个item节点,寻找其中的label节点,并且存储label的firstChild 6. if (str != "") { var tempDiv = document.createElement("div"); tempDiv.innerHTML = thisState; tempDiv.onclick = makeChoice; tempDiv.className = "suggestions"; document.getElementById("popups").appendChild(tempDiv); 因为这个州名是一个可能值,我们希望将它添加到要显示的列表中。实现方法是,创建一个临时 if (foundCt == 1) { document.getElementById("searchField").value = document.getElementById ➝("popups").firstChild.innerHTML; document.getElementById("popups").innerHTML = ""; } 如果foundCt是1,我们就知道找到了唯一的匹配,所以可以将这个州名放进字段。如果用户已 function makeChoice(evt) { if (evt) { var thisDiv = evt.target; } else { var thisDiv = window.event.srcElement; } document.getElementById("searchField").value = thisDiv.innerHTML; document.getElementById("popups").innerHTML = ""; } 输入州名的另一种方法是,单击弹出列表中的一个州名。在这种情况下,会调用makeChoice()事 |
请发表评论