时间:2021-07-01 10:21:17 帮助过:41人阅读
if(!sx)
var sx={};
sx.activex={};
sx.activex.autocomplete={
bind:function(a,s){
var d=document.createElement("div");
d.style.position="absolute";
d.flag="autocomplete";
document.body.appendChild(d);
d.style.left="-1000px";
d.style.height="100px";
d.style.overflow="auto";
a.onblur=function(){
if(document.elementFromPoint(window.event.clientX,window.event.clientY).flag=="autocomplete" || document.elementFromPoint(window.event.clientX,window.event.clientY).parentNode.flag=="autocomplete")
return;
d.innerHTML="";
d.style.left="-1000px";
}
a.onkeyup=function(){
if(a.value=="") {
d.innerHTML="";
return;
}
d.innerHTML="";
var t=[];
for(var i in s){
if(eval("/^"+a.value+"$/i").test(s[i])){
t.push(s[i]);
}
}
for(var i in s){
if(eval("/^"+a.value+".+$/i").test(s[i])){
t.push(s[i]);
}
}
for(var i in s){
if(eval("/^.+"+a.value+"$/i").test(s[i])){
t.push(s[i]);
}
}
for(var i in s){
if(eval("/^.+"+a.value+".+$/i").test(s[i])){
t.push(s[i]);
}
}
for(var e=0;e<=t.length-2;e++){
for(var e1=e+1;e1<=t.length-1;e1++){
if(t[e]==t[e1]){
for(var e2=e1+1;e2<t.length;e2++){
if(t[e2]){
t[e2-1]=t[e2];
}
}
t.length=t.length-1;
}
}
}
//alert(t);
for(var i in t){
var p=document.createElement("div");
p.innerText=t[i];
p.onmouseenter=function(){
this.style.backgroundColor="blue";
}
p.onmouseleave=function(){
this.style.backgroundColor="white";
}
p.onclick=function(){
a.value=this.innerText;
d.style.left="-1000px";
}
d.appendChild(p)
}
d.style.top=a.offsetTop+a.offsetHeight+"px";
d.style.left=a.offsetLeft+"px";
d.style.width=a.offsetWidth+"px";
}
}
}.
调用的html代码:
代码如下:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script src="kongjian.js"></script>
<input type="text" size="15" id="a">
<input type="text" size="15" id="a1">
<script>
sx.activex.autocomplete.bind(document.getElementById("a"),["asd","a","sad","er","ewrew","aadasd","wqqwrqw","asasf","qweqw"]);
sx.activex.autocomplete.bind(document.getElementById("a1"),["asd","a","sad","er","ewrew","aadasd","wqqwrqw","asasf","qweqw"]);
</script>
</body>
</html>
代码没有优化,还请多多包涵,这里给出一个思路,让各位见笑了.