autocomplete

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js简单的自动完成功能</title>
<script type="text/javascript">
var test = function(){
if(document.getElementById('dis')){
document.getElementById('dis').parentNode.removeChild(document.getElementById('dis'));
};
var arr = ['adsd-in','aads-dd','aasd-in','aa-dd','b-s','c-e','csd'];
var txt = document.getElementById('txt');
var re = new RegExp("^"+txt.value+"\w*");
var div = document.createElement('div');
div.id = 'dis';
var innerDiv;
for(var i=0; i<arr.length; i++){
if(re.test(arr[i])){
innerDiv = document.createElement('div');
innerDiv.className = 'innerDiv';
innerDiv.appendChild(document.createTextNode(arr[i]));
innerDiv.onclick = function(){
this.parentNode.previousSibling.value = this.innerHTML;
document.getElementById('dis').parentNode.removeChild(document.getElementById('dis'));
};
innerDiv.onmouseover = function(){
this.style.backgroundColor = '#ff0000';
};
innerDiv.onmouseout = function(){
this.style.backgroundColor = '#cccccc';
};
div.appendChild(innerDiv);
};
};
if(div.hasChildNodes()){
txt.parentNode.insertBefore(div, txt.nextSibling || null);
};
}
</script>
<style type="text/css">
#dis{
width:153px;
background:#CCCCCC;
border:1px solid #666;
height: 60px;
overflow-y: scroll;
}
.innerDiv{
border-top:1px solid #666;
}
</style>
</head>
<body>
<input type="text" id="txt" onkeyup="test();" />
</body>
</html>

本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

posted @ 2006-09-22 23:24 音乐啤酒 阅读(...) 评论(...) 编辑 收藏