复制代码 代码如下:
<SCRIPTlanguage=JScripttype=text/javascript>
varisOpera=navigator.userAgent.indexOf("Opera")>-1;
varisIE=navigator.userAgent.indexOf("MSIE")>1&&!isOpera;
varisMoz=navigator.userAgent.indexOf("Mozilla/5.")==0&&!isOpera;
functiontextboxSelect(oTextbox,iStart,iEnd){
switch(arguments.length){
case1:
oTextbox.select();
break;
case2:
iEnd=oTextbox.value.length;
/*fallsthrough*/
case3:
if(isIE){
varoRange=oTextbox.createTextRange();
oRange.moveStart("character",iStart);
oRange.moveEnd("character",-oTextbox.value.length+iEnd);
oRange.select();
}elseif(isMoz){
oTextbox.setSelectionRange(iStart,iEnd);
}
}
oTextbox.focus();
}
/*
functiontextboxReplaceSelect(oTextbox,sText){
if(isIE){
varoRange=oTextbox.createTextRange();
oRange.text=sText;
oRange.collapse(true);
oRange.select();
}elseif(isMoz){
variStart=oTextbox.selectionStart;
oTextbox.value=oTextbox.value.substring(0,iStart)+sText+oTextbox.value.substring(oTextbox.selectionEnd,oTextbox.value.length);
oTextbox.setSelectionRange(iStart+sText.length,iStart+sText.length);
}
oTextbox.focus();
}
*/
functionautocompleteMatch(sText,arrValues){
for(vari=0;i<arrValues.length;i++){
if(arrValues[i].indexOf(sText)==0){
returnarrValues[i];
}
}
returnnull;
}
functionautocomplete(oTextbox,oEvent,arrValues){
switch(oEvent.keyCode){
case38://uparrow
case40://downarrow
case37://leftarrow
case39://rightarrow
case33://pageup
case34://pagedown
case36://home
case35://end
case13://enter
case9://tab
case27://esc
case16://shift
case17://ctrl
case18://alt
case20://capslock
case8://backspace
case46://delete
returntrue;
break;
default:
//下面这一行用处不大(被注释)
//textboxReplaceSelect(oTextbox,isIE?oTextbox.value/*oEvent.keyCode*/:oEvent.charCode);
variLen=oTextbox.value.length;
varsMatch=autocompleteMatch(oTextbox.value,arrValues);
if(sMatch!=null){
oTextbox.value=sMatch;
textboxSelect(oTextbox,iLen,oTextbox.value.length);
}
returnfalse;
}
}
</SCRIPT>
<SCRIPT>
vararrValues=["red","orange","yellow","green","blue","indigo","violet","brown"];
</SCRIPT>
<H2>AutocompleteTextboxExample</H2>
<P>Typeinacolorinlowercase:输入一个以小写字母开头的颜色(英文单词,比如:r、b等)<BR><INPUTid=txt1onkeyup="returnautocomplete(this,event,arrValues)"></P>