>웹 프론트엔드 >JS 튜토리얼 >JS_javascript 기술로 구현된 간단한 자동 완성 예

JS_javascript 기술로 구현된 간단한 자동 완성 예

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB원래의
2016-05-16 16:52:151713검색

우연히 발견한 자동완성 소스코드를 공유합니다. 여기서 테스트할 때는 배열을 사용하는데, 실제로 사용할 때는 Ajax를 통해 서버에서 가져오는 방식으로 변경하면 괜찮을 것 같습니다.

팁: HTML 파일로 직접 저장하여 효과를 볼 수 있습니다.

코드 복사 코드는 다음과 같습니다.




<script><BR><!--<BR>var $ = function (id) {<BR>    return "string" == typeof id ? document.getElementById(id) : id;<BR>}<BR>var Bind = function(object, fun) {<BR>    return function() {<BR>        return fun.apply(object, 인수);<BR> }<BR>}<BR>function AutoComplete(obj,autoObj,arr){<BR>    this.obj=$(obj);        //输入框<BR>    this.autoObj=$(autoObj);//DIV적 根节点<BR>    this.value_arr=arr;        //부담이 심함<BR>    this.index=-1;          //当前选中的DIV的索引<BR>    this.search_value="";   //保存当前搜索的字符<BR>}<BR>AutoComplete.prototype={<BR>    //初始化DIV적位置<BR>    init: function(){<BR>        this.autoObj.style.left = this. obj.offsetLeft "px";<BR>        this.autoObj.style.top  = this.obj.offsetTop this.obj.offsetHeight "px";<BR>        this.autoObj.style.width= this.obj.offsetWidth - 2 "px"; // 减去边框的长度 2px <BR>}, <🎜 🎜 // 删除自动完成需要的所有 div <BR> deletediv : function () {<BR> while (this.autoobj.haschildnodes () ){<BR>            this.autoObj.removeChild(this.autoObj.firstChild);<BR>        }<BR>        this.autoObj.className="auto_hidden";<BR>    },<BR>    //设置值<BR> setValue: function(_this){<BR>        반환 함수(){<BR>           _this.obj.value=this.seq;<BR>           _this.autoObj.className="auto_hidden";<BR>        }      <BR>   }, <BR>    //模拟鼠标移动至DIV时,DIV高亮<BR>    autoOnmouseover: function(_this,_div_index){<BR>        return function(){<BR>           _this.index=_div_index;<BR>            변수 길이 = _this.autoObj.children.length;<BR>            for(var j=0;j<length;j ){<BR>               if(j!=_this.index ){      <BR>                  _this.autoObj.childNodes[ j]. className='auto_onmouseout';<BR>               }else{<BR>                  _this.autoObj.childNodes[j].className='auto_onmouseover';<BR>              }<BR>            }<BR>        }<BR>    },<BR>    //更改classname<BR>   changeClassname: function(length){<BR>        for(var i=0;i<length;i ){<BR>            if(i!=this.index ){      <BR>               this.autoObj.childNodes[i].className='auto_onmouseout';<BR>           }else{<BR>              this.autoObj.childNodes[i].className='auto_onmouseover' ;<BR>                this.obj .value = this.autoobj.childnodes [i] .seq; <BR>} <BR>} <BR>} <🎜 <BR>, <🎜 🎜> // 响应键盘 <BR> presskey : function (event) {<🎜 🎜> var length = this.autoObj.children.length;<BR>        //光标键"↓"<BR>        if(event.keyCode==40){<BR>            this.index;<BR>            if(this.index> 길이){<BR>                this.index=0;<BR>            }else if(this.index==length){<BR>               this.obj.value=this.search_value;<BR>           }<BR>            this.changeClassname (length);<BR>        }<BR>        //光标键"↑"<BR>        else if(event.keyCode==38){<BR>           this.index--;<BR>            if(this.index< -1){<BR>                this.index=length - 1;<BR>            }else if(this.index==-1){<BR>               this.obj.value=this.search_value;<BR>            }<BR>            this.changeClassname(length);<BR>        }<BR>        //回车键<BR>        else if(event.keyCode==13){<BR>            this.autoObj.className="auto_hidden";<BR> this.index=-1;<BR>        }else{<BR>            this.index=-1;<BR>        }<BR>    },<BR>    //程序入口<BR>    start: function(event){<BR>        if(event.keyCode!=13&&event.keyCode!=38&&event.keyCode!=40){<BR>           this.init();<BR>            this.deleteDIV();<BR>            this.search_value=this .obj .value;<BR>            var valueArr=this.value_arr;<BR>            valueArr.sort();<BR>            if(this.obj.value.replace(/(^s*)|(s*$)/g, '')==""){ 반환; }//值为空,退 Out<BR>            try{ var reg = new RegExp("(" this.obj.value ")","i");}<BR>            catch (e){ return; }<BR> var div_index=0;//생성된 DIV의 인덱스 기록<BR> for(var i=0;i<valueArr.length;i){<BR> if(reg.test(valueArr[i]) ) {<BR> var div = document.createElement("div");<BR> div.className="auto_onmouseout";<BR> div.seq=valueArr[i];<BR>      div.onclick=this.setValue ( this);<BR> div.onmouseover=this.autoOnmouseover(this,div_index);<BR> div.innerHTML=valueArr[i].replace(reg,"<strong>$1"); / /검색된 문자는 굵게 표시됩니다<BR> this.autoObj.appendChild(div);<BR> this.autoObj.className="auto_show";<BR> div_index;<BR>                                                                                            <BR> this.pressKey(이벤트 );<BR> window.onresize=Bind(this,function(){this.init();});<BR> }<BR>}<BR>//--&gt ;<BR></script>> ;

자동 완성 기능


div>

<script><BR> var autoComplete= new AutoComplete('o','auto',['b0' ,'b12','b22','b3','b4','b5','b6','b7','b8','b2','abd','ab','acd',' accd','b1','cd','ccd','cbcv','cxf']);<BR></script>




성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.