天天看點

js自動補全

原生js的自動補全,不依賴jquery,樣式大家可以自己根據自己的需要修改,以下供大家參考

點選下載下傳案例代碼

<!doctype html>
<html>
<meta charset="utf-8">

<!--=================== 封裝開始 ======================-->
<!--樣式-->
<style>
    .auto_hidden {
        width:px;border-top: px solid #333;
        border-bottom: px solid #333;
        border-left: px solid #333;
        border-right: px solid #333;
        position:absolute;
        display:none;
    }
    .auto_show {
        width:px;
        border-top: px solid #333;
        border-bottom: px solid #333;
        border-left: px solid #333;
        border-right: px solid #333;
        position:absolute;
        z-index:; /* 設定對象的層疊順序 */
        display:block;
    }
    .auto_onmouseover{
        color:#ffffff;
        background-color:highlight;
        width:%;
    }
    .auto_onmouseout{
        color:#000000;
        width:%;
        background-color:#ffffff;
    }
</style>

<!--js檔案-->
<script language="javascript">

    var $id = function (id) {
        return "string" == typeof id ? document.getElementById(id) : id;
    }

    var Bind = function(object, fun) {
        return function() {
            return fun.apply(object, arguments);
        }
    }

    function AutoComplete(obj,arr){
        this.obj=$id(obj);//輸入框
        var autodiv = document.createElement("div");//建立一個元素節點,自動完成DIV
        autodiv.setAttribute("id","autoComplete_div");//設定元素id
        autodiv.setAttribute("class","auto_hidden");//設定元素class
        this.after(autodiv);//在目前元素後面追加
        this.autoObj=$id("autoComplete_div");//DIV的根節點
        this.value_arr=arr;         //不要包含重複值
        this.index=-;             //目前選中的DIV的索引
        this.search_value="";     //儲存目前搜尋的字元
        this.start();//調用程式入口方法
    }

    AutoComplete.prototype={

        init: function(){//初始化DIV的位置

            this.autoObj.style.left = this.obj.offsetLeft + "px";
            this.autoObj.style.top  = this.obj.offsetTop + this.obj.offsetHeight + "px";
            this.autoObj.style.width= this.obj.offsetWidth -  + "px";//減去邊框的長度2px

        },deleteDIV: function(){//删除自動完成需要的所有DIV

            while(this.autoObj.hasChildNodes()){
                this.autoObj.removeChild(this.autoObj.firstChild);
            }
            this.autoObj.className="auto_hidden";

        },setValue: function(_this){//設定值

            return function(){
                _this.obj.value=this.seq;
                _this.autoObj.className="auto_hidden";
            }

        },autoOnmouseover: function(_this,_div_index){//模拟滑鼠移動至DIV時,DIV高亮

            return function(){
                _this.index=_div_index;
                var length = _this.autoObj.children.length;
                for(var j=;j<length;j++){
                    if(j!=_this.index ){
                        _this.autoObj.childNodes[j].className='auto_onmouseout';
                    }else{
                        _this.autoObj.childNodes[j].className='auto_onmouseover';
                    }
                }
            }

        },changeClassname: function(length){//更改classname

            for(var i=;i<length;i++){
                if(i!=this.index ){
                    this.autoObj.childNodes[i].className='auto_onmouseout';
                }else{
                    this.autoObj.childNodes[i].className='auto_onmouseover';
                    this.obj.value=this.autoObj.childNodes[i].seq;
                }
            }

        },pressKey: function(event){//響應鍵盤

            var length = this.autoObj.children.length;
            if(event.keyCode==){//光标鍵"↓"
                ++this.index;
                if(this.index>length){
                    this.index=;
                }else if(this.index==length){
                    this.obj.value=this.search_value;
                }
                this.changeClassname(length);

            }else if(event.keyCode==){//光标鍵"↑"
                this.index--;
                if(this.index<-){
                    this.index=length - ;
                }else if(this.index==-){
                    this.obj.value=this.search_value;
                }
                this.changeClassname(length);

            }else if(event.keyCode==){//Enter鍵
                this.autoObj.className="auto_hidden";
                this.index=-;

            }else{
                this.index=-;
            }
        },after:function (newElement){//newElement是要追加的元素
            var parent = this.obj.parentNode;//找到目前元素的父節點 
            if( parent.lastChild == this.obj ){//判斷目前元素是否是節點中的最後一個位置,如果是的話就直接使用appendChild方法 
                parent.appendChild(newElement,this.obj ); 
            }else{ 
                parent.insertBefore(newElement,this.obj.nextSibling ); 
            }; 

        },start: function(){//程式入口
            var thisObj=this;
            thisObj.obj.onkeyup=function(event){
                 if(event.keyCode!=&&event.keyCode!=&&event.keyCode!=){
                    thisObj.init();
                    thisObj.deleteDIV();
                    thisObj.search_value=thisObj.obj.value;
                    var valueArr=thisObj.value_arr;
                    valueArr.sort();
                    if(thisObj.obj.value.replace(/(^\s*)|(\s*$)/g,'')==""){ return; }//值為空,退出
                    try{ 
                        var reg = new RegExp("(" + thisObj.obj.value + ")","i");
                    }catch (e){
                        return; 
                    }
                    var div_index=;//記錄建立的DIV的索引
                    for(var i=;i<valueArr.length;i++){
                        if(reg.test(valueArr[i])){
                            var div = document.createElement("div");
                            div.className="auto_onmouseout";
                            div.seq=valueArr[i];
                            div.onclick=thisObj.setValue(thisObj);
                            div.onmouseover=thisObj.autoOnmouseover(thisObj,div_index);
                            div.innerHTML=valueArr[i].replace(reg,"<strong>$1</strong>");//搜尋到的字元粗體顯示
                            thisObj.autoObj.appendChild(div);
                            thisObj.autoObj.className="auto_show";
                            div_index++;
                        }
                    }
                }
                thisObj.pressKey(event);
                window.onresize=Bind(thisObj,function(){thisObj.init();});
            };
        }
    }
</script>
<!--=================== 封裝結束 ======================-->




<!--========================== 調用開始 =========================-->
<body>
    <!--第一個自動補全-->
    <input type="text"  placeholder="請輸入a或b模拟效果" id="o" >

    <!--第二個自動補全-->
    <input type="text"  placeholder="請輸入或2模拟效果" id="o1" >

<script>
    //執行個體化第一個自動補全,參數一 為元素id,參數二 為資料
    new AutoComplete('o',['b0','b12','b22','b3','b4','b5','b6','b7','b8','b2','abd','ab','acd','accd','b1','cd','ccd','cbcv','cxf']);
    //執行個體化第二個自動補全,參數一 為元素id,參數二 為資料
    new AutoComplete('o1',[,'22','33','13','123','1221','23','234','256','s2','12s','12sfd','1346','a22d','b2','2cd','c1cd','cb1cv']);
</script>

</body>
<!--========================== 調用結束 =========================-->


</html>
           

效果如下:

js自動補全