<html>
<head>
<title>Ajax实现自动提示的文本框</title>
<style>
<!--
body{
font-family:Arial, Helvetica, sans-serif;
font-size:12px; padding:0px; margin:5px;
}
form{padding:0px; margin:0px;}
input{
font-family:Arial, Helvetica, sans-serif;
font-size:12px; border:1px solid #000000;
200px; padding:1px; margin:0px;
}
#popup{
position:absolute; 202px;
color:#004a7e; font-size:12px;
font-family:Arial, Helvetica, sans-serif;
left:41px; top:25px;
}
#popup.show{
border:1px solid #004a7e;
}
#popup.hide{
border:none;
}
ul{
list-style:none;
margin:0px; padding:0px;
}
li.mouseOver{
background-color:#004a7e;
color:#FFFFFF;
}
li.mouseOut{
background-color:#FFFFFF;
color:#004a7e;
}
-->
</style>
<script language=
"javascript"
>
var oInputField;
var oPopDiv;
var oColorsUl;
var xmlHttp;
function createXMLHttpRequest(){
if
(window.ActiveXObject)
xmlHttp =
new
ActiveXObject(
"Microsoft.XMLHTTP"
);
else
if
(window.XMLHttpRequest)
xmlHttp =
new
XMLHttpRequest();
}
function initVars(){
oInputField = document.forms[
"myForm1"
].colors;
oPopDiv = document.getElementById(
"popup"
);
oColorsUl = document.getElementById(
"colors_ul"
);
}
function clearColors(){
for
(var i=oColorsUl.childNodes.length-1;i>=0;i--)
oColorsUl.removeChild(oColorsUl.childNodes[i]);
oPopDiv.className =
"hide"
;
}
function setColors(the_colors){
clearColors();
oPopDiv.className =
"show"
;
var oLi;
for
(var i=0;i<the_colors.length;i++){
oLi = document.createElement(
"li"
);
oColorsUl.appendChild(oLi);
oLi.appendChild(document.createTextNode(the_colors[i]));
oLi.onmouseover = function(){
this
.className =
"mouseOver"
;
}
oLi.onmouseout = function(){
this
.className =
"mouseOut"
;
}
oLi.onclick = function(){
oInputField.value =
this
.firstChild.nodeValue;
clearColors();
}
}
}
function findColors(){
initVars();
if
(oInputField.value.length > 0){
createXMLHttpRequest();
var sUrl =
"9-10.aspx?sColor="
+ oInputField.value +
"×tamp="
+
new
Date().getTime();
xmlHttp.open(
"GET"
,sUrl,
true
);
xmlHttp.onreadystatechange = function(){
if
(xmlHttp.readyState == 4 && xmlHttp.status == 200){
var aResult =
new
Array();
if
(xmlHttp.responseText.length){
aResult = xmlHttp.responseText.split(
","
);
setColors(aResult);
}
else
clearColors();
}
}
xmlHttp.send(
null
);
}
else
clearColors();
}
</script>
</head>
<body>
<form method=
"post"
name=
"myForm1"
>
Color: <input type=
"text"
name=
"colors"
id=
"colors"
onkeyup=
"findColors();"
/>
</form>
<div id=
"popup"
>
<ul id=
"colors_ul"
></ul>
</div>
</body>
</html>