茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    原生js編寫(xiě)autoComplete插件
    來(lái)源:易賢網(wǎng) 閱讀:1158 次 日期:2016-07-08 11:34:08
    溫馨提示:易賢網(wǎng)小編為您整理了“原生js編寫(xiě)autoComplete插件”,方便廣大網(wǎng)友查閱!

    這篇文章主要為大家詳細(xì)介紹了原生js編寫(xiě)的autoComplete插件,感興趣的小伙伴們可以參考一下

    最近有提關(guān)于下拉選項(xiàng)過(guò)多的時(shí)候,希望輸入關(guān)鍵詞,可以搜索內(nèi)容的需求,但是之前項(xiàng)目太趕,所以也就沒(méi)有來(lái)得及做,因?yàn)橄M迷鷍s寫(xiě)一些內(nèi)容,所以插件是采用了原生js寫(xiě)的思路如下

    第一步:fnInit實(shí)現(xiàn)初始化一些字段

    第二步:加載搜索框的div

    第三步:實(shí)現(xiàn)search功能,刪除原節(jié)點(diǎn)并加載新節(jié)點(diǎn)

    第四步:點(diǎn)擊或者回車(chē)的時(shí)候設(shè)置value

    代碼:

    autoComplete.js

    /** 

     * @summary   AutoComplete 

     * @description 輸入框自動(dòng)檢索下拉選項(xiàng) 

     * @version   0.0.1 

     * @file     autoComplete.js 

     * @author   cangowu 

     * @contact   1138806090@qq.com 

     * @copyright  Copyright 2016 cangoWu. 

     * 

     * 這是一個(gè)基于原生js的自動(dòng)完成搜索的下拉輸入框, 

     * 可以通過(guò)移動(dòng)鼠標(biāo)上下鍵回車(chē)以及直接用鼠標(biāo)點(diǎn)擊 

     * 選中搜索的選項(xiàng),在一些關(guān)鍵的地方都有注釋 

     * 

     * 實(shí)例參見(jiàn): 

     * CSDN博客:http://blog.csdn.net/wzgdjm/article/details/51122615 

     * Github:https://github.com/cangowu/autoComplete 

     * 

     */

    (function () { 

      function AutoComplete() { 

        if (!(this instanceof AutoComplete)) { 

          return new AutoComplete(); 

        } 

        this.sSearchValue = ''; 

        this.index = -1; 

      } 

      AutoComplete.prototype = { 

        fnInit: function (option) {//初始化基本信息 

          var oDefault = { 

            id: '', //控件id 

            data: [], //數(shù)據(jù) 

            paraName: '', 

            textFiled: '', //顯示的文字的屬性名 

            valueFiled: '', //獲取value的屬性名 

            style: {}, //顯示的下拉div的樣式設(shè)置 

            url: '', //ajax請(qǐng)求的url 

            select: function () { 

            }, //選擇選項(xiàng)時(shí)觸發(fā)的事件 

          }; 

          var _option = option; 

          this.sId = _option.id || oDefault.id; 

          this.aData = _option.data || oDefault.data; 

          this.paraName = _option.paraName || oDefault.paraName; 

          this.sTextFiled = _option.textFiled || oDefault.textFiled; 

          this.sValueFiled = _option.valueFiled || oDefault.valueFiled; 

          this.style = _option.style || oDefault.style; 

          this.sUrl = _option.url || oDefault.url; 

          this.fnSelect = _option.select || oDefault.select; 

          this.sDivId = this.sId + new Date().getTime();//加載選項(xiàng)額divid 

          //判斷如果傳入了url,沒(méi)有傳入data數(shù)據(jù),就ajax獲取數(shù)據(jù),否則使用data取數(shù)據(jù) 

          if (this.sUrl !== '' && this.aData.length === 0) { 

            var that = this; 

            this.util.fnGet(this.sUrl, function (data) { 

              console.log(eval(data)); 

              that.aData = eval(data); 

            }, 10); 

          } 

          //給aData排序 

          var sTextField = this.sTextFiled; 

          this.aData.sort(function (a, b) { 

            return a[sTextField] > b[sTextField]; 

          }); 

          //獲取控件 

          this.domInput = document.getElementById(this.sId); 

          //this.domDiv = document.getElementById(this.sDivId); 

        }, 

        fnRender: function () {//渲染一些必須的節(jié)點(diǎn) 

          var that = this; 

          //生成一個(gè)對(duì)應(yīng)的div,承載后面的一些選項(xiàng)的 

          if (that.sDivId) { 

            var domDiv = document.createElement('div'); 

            domDiv.id = that.sDivId; 

            domDiv.style.background = '#fff'; 

            domDiv.style.width = that.domInput.offsetWidth - 2 + 'px'; 

            domDiv.style.position = 'absolute'; 

            domDiv.style.border = '1px solid #a9a9a9'; 

            domDiv.style.display = 'none'; 

            that.util.fnInsertAfter(domDiv, that.domInput); 

            //加載之后才能將domDiv賦值為 

            this.domDiv = document.getElementById(this.sDivId); 

          } 

          //給input添加keyup事件 

          that.util.fnAddEvent(that.domInput, 'keyup', function (event) { 

            that.fnSearch(event); 

          }); 

        }, 

        fnSearch: function (event) { 

          //判斷如果不是回車(chē)鍵,上鍵下鍵的時(shí)候執(zhí)行搜索 

          if (event.keyCode != 13 && event.keyCode != 38 && event.keyCode != 40) { 

            this.fnLoadSearchContent(); 

            this.fnShowDiv(); 

          } else {//搜索之后監(jiān)測(cè)鍵盤(pán)事件 

            var length = this.domDiv.children.length; 

            if (event.keyCode == 40) { 

              ++this.index; 

              if (this.index >= length) { 

                this.index = 0; 

              } else if (this.index == length) { 

                this.domInput.value = this.sSearchValue; 

              } 

              this.domInput.value = this.domDiv.childNodes[this.index].text; 

              this.fnChangeClass(); 

            } 

            else if (event.keyCode == 38) { 

              this.index--; 

              if (this.index <= -1) { 

                this.index = length - 1; 

              } else if (this.index == -1) { 

                this.obj.value = this.sSearchValue; 

              } 

              this.domInput.value = this.domDiv.childNodes[this.index].text; 

              this.fnChangeClass(); 

            } 

            else if (event.keyCode == 13) { 

              this.fnLoadSearchContent(); 

              this.fnShowDiv(); 

              //this.domDiv.style.display = this.domDiv.style.display === 'none' ? 'block' : 'none'; 

              this.index = -1; 

            } else { 

              this.index = -1; 

            } 

          } 

        }, 

        fnLoadSearchContent: function () { 

          //刪除所有的子節(jié)點(diǎn) 

          while (this.domDiv.hasChildNodes()) { 

            this.domDiv.removeChild(this.domDiv.firstChild); 

          } 

          //設(shè)置search的值 

          this.sSearchValue = this.domInput.value; 

          //如果值為空的時(shí)候選擇退出 

          var sTrimSearchValue = this.sSearchValue.replace(/(^\s*)|(\s*$)/g, ''); 

          if (sTrimSearchValue == "") { 

            this.domDiv.style.display = 'none'; 

            return; 

          } 

          try { 

            var reg = new RegExp("(" + sTrimSearchValue + ")", "i"); 

          } 

          catch (e) { 

            return; 

          } 

          //搜索并增加新節(jié)點(diǎn) 

          var nDivIndex = 0; 

          for (var i = 0; i < this.aData.length; i++) { 

            if (reg.test(this.aData[i][this.sTextFiled])) { 

              var domDiv = document.createElement("div"); 

              //div.className="auto_onmouseout"; 

              domDiv.text = this.aData[i][this.sTextFiled]; 

              domDiv.onclick = this.fnSetValue(this); 

              domDiv.onmouseover = this.fnAutoOnMouseOver(this, nDivIndex); 

              domDiv.innerHTML = this.aData[i][this.sTextFiled].replace(reg, "<strong>$1</strong>");//搜索到的字符粗體顯示 

              this.domDiv.appendChild(domDiv); 

              nDivIndex++; 

            } 

          } 

        }, 

        fnSetValue: function (that) { 

          return function () { 

            that.domInput.value = this.text; 

            that.domDiv.style.display = 'none'; 

          } 

        }, 

        fnAutoOnMouseOver: function (that, idx) { 

          return function () { 

            that.index = idx; 

            that.fnChangeClass(); 

          } 

        }, 

        fnChangeClass: function () { 

          var that = this; 

          var length = that.domDiv.children.length; 

          for (var j = 0; j < length; j++) { 

            if (j != that.index) { 

              that.domDiv.childNodes[j].style.backgroundColor = ''; 

              that.domDiv.childNodes[j].style.color = '#000'; 

            } else { 

              that.domDiv.childNodes[j].style.backgroundColor = 'blue'; 

              that.domDiv.childNodes[j].style.color = '#fff'; 

            } 

          } 

        }, 

        fnShowDiv: function () { 

          if (this.domDiv.children.length !== 0) { 

            this.domDiv.style.display = this.domDiv.style.display === 'none' ? 'block' : 'none'; 

          } 

        }, 

        util: {//公共接口方法 

          fnInsertAfter: function (ele, targetEle) { 

            var parentnode = targetEle.parentNode || targetEle.parentElement; 

            if (parentnode.lastChild == targetEle) { 

              parentnode.appendChild(ele); 

            } else { 

              parentnode.insertBefore(ele, targetEle.nextSibling); 

            } 

          }, 

          fnAddEvent: function (ele, evt, fn) { 

            if (document.addEventListener) { 

              ele.addEventListener(evt, fn, false); 

            } else if (document.attachEvent) { 

              ele.attachEvent('on' + (evt == "input" ? "propertychange" : evt), fn); 

            } else { 

              ele['on' + (evt == "input" ? "propertychange" : evt)] = fn; 

            } 

          }, 

          fnGet: function (url, fn, timeout) { 

            var xhr = null; 

            try { 

              if (window.XMLHttpRequest) { 

                xhr = new XMLHttpRequest(); 

              } else if (Window.ActiveXObject) { 

                xhr = new ActiveXObject("Msxml2.Xmlhttp"); 

              } 

            } catch (e) { 

              //TODO handle the exception 

              xhr = new ActiveXObject('Microsoft.Xmlhttp'); 

            } 

            xhr.onreadystatechange = function () { 

              if (this.readyState == 4 && this.status == 200) { 

                fn.call(this, this.responseText); 

              } else { 

                setTimeout(function () { 

                  xhr.abort(); 

                }, timeout); 

              } 

            }; 

            xhr.open('get', url, true); 

            xhr.send(); 

          } 

        } 

      } 

      window.AutoComplete = function (option) { 

        var aOption = Array.prototype.slice.call(arguments); 

        for(var i=0;i<aOption.length;i++){ 

          var autoComplete = new AutoComplete(); 

          autoComplete.fnInit(aOption[i]); 

          autoComplete.fnRender(); 

        } 

      } 

    })(window);

    index.html

    <!DOCTYPE html> 

    <html lang="en"> 

    <head> 

      <meta charset="UTF-8"> 

      <title>Title</title> 

    </head> 

    <body> 

    <div> 

    <input type="text" id="txtTest"> 

    </div> 

    <br> 

    <div> 

    <input type="text" id="txtTest1"> 

    </div> 

    <script src="autoComplete.js"></script> 

    <script> 

      window.onload = function () { 

        var option = { 

          id: 'txtTest', //控件id 

          data: [{ 

            "id": "1", 

            "name": "aaaaa" 

          }, { 

            "id": "2", 

            "name": "bbbbb" 

          }, { 

            "id": "2", 

            "name": "bbb吳bb" 

          }, { 

            "id": "2", 

            "name": "bbbzbb" 

          }], 

          paraName: 'name', 

          textFiled: 'name', //顯示的文字的屬性名 

          valueFiled: 'id', //獲取value的屬性名 

          select: function (val, text) { 

            alert(val + '' + text); 

          } //選擇選項(xiàng)時(shí)觸發(fā)的事件 

        }; 

        var option1 = { 

          id: 'txtTest1', //控件id 

          url: 'data.json', //數(shù)據(jù) 

          paraName: 'name', 

          textFiled: 'name', //顯示的文字的屬性名 

          valueFiled: 'id', //獲取value的屬性名 

          select: function (val, text) { 

            alert(val + '' + text); 

          } //選擇選項(xiàng)時(shí)觸發(fā)的事件 

        }; 

        AutoComplete(option,option1); 

      } 

    </script> 

    </body> 

    </html>

    data.json

     { 

      "id": "1", 

      "name": "aaaaa"

     }, 

     { 

      "id": "2", 

      "name": "bbbbb"

     }, 

     { 

      "id": "3", 

      "name": "ccccc"

     } 

    ]

    以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:原生js編寫(xiě)autoComplete插件
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類(lèi)型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專(zhuān)用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
    咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)