標籤:style blog http color 使用 os
js代碼:
/*create by code_bunny 20140701 [email protected] https://github.com/OOP-Code-Bunny*/(function ($) { $.fn.autoComplate = function (opts) { this.each(function () { init.call(this, opts); }); return this; }; function init(opts) { var defaultOption = { textInput: $(this), //文本輸入框 emailBox: $(this).next(), //用於放置聯想郵箱的框 links:[‘163.com‘,‘qq.com‘,‘126.com‘,‘gmail.com‘,‘yeah.net‘,‘hotmail.com‘,‘sina.com‘,‘sina.cn‘,‘sohu.com‘] //聯想內容, }; var options = $.extend(defaultOption, opts); var func = {}; func.init = function(){ this.timeOut = null; this.cur = null; this.emailList = []; this.aUl = $(‘<ul>‘); for(var i=0; i<this.links.length; i++){ var aLi = $(‘<li>‘); this.emailList.push(aLi); this.aUl.append(aLi); aLi.bind(‘mousedown‘,func.chooseLi.bind(options)); } this.emailBox.append(this.aUl); this.textInput.bind(‘keydown‘,func.keyDownFun.bind(this)).bind(‘keyup‘,func.keyUpFun.bind(this)).bind(‘blur‘,func.textBlur.bind(this)); return this; }; func.chooseLi = function(e){ this.textInput.val($(e.target).text()); }; func.keyDownFun = function(e){ func.clearTO.call(this); if(e.keyCode==13){ return func.pressEnter.call(this); } else if (e.keyCode==40) { return func.upAndDown.apply(this,[true]); } else if(e.keyCode==38){ return func.upAndDown.apply(this,[false]); } this.timeOut = setTimeout(func.timeout.bind(this),0) }; func.keyUpFun = function(e){ func.clearTO.call(this); if (func.ifNotText.apply(this, [e.keyCode])) return; this.timeOut = setTimeout(func.timeout.bind(this),0) }; func.pressEnter = function(){ return (this.cur === null) ? func.noCurPressEnter.call(this) : func.curPressEnter.call(this) }; func.noCurPressEnter = function(){ return (this.textInput.val().indexOf(‘@‘)>0 && this.textInput.val().indexOf(‘@‘)<this.textInput.val().length-1) ? this.textInput.blur() : this.textInput.val(this.emailList[0].text()).blur(); }; func.curPressEnter = function(){ return this.textInput.val(this.emailBox.find(‘li:visible‘).eq(this.cur).text()).blur(); }; //上下鍵函數,參數表示上下,true為下,false為上 func.upAndDown = function(direct){ this.length = this.emailBox.find(‘li:visible‘).length; var start = direct ? 0 : this.length-1; if(this.cur === null) { this.cur = start; this.emailBox.find(‘li:visible‘).eq(this.cur).addClass(‘cur‘); } else { func.removeCur.call(this); var end = direct ? options.length-1 : 0; this.cur = this.cur == end ? start : (direct ? this.cur+1 : this.cur-1); this.emailBox.find(‘li:visible‘).eq(this.cur).addClass(‘cur‘); } }; func.timeout = function(){ return this.textInput.val() == ‘‘ ? this.emailBox.hide() : func.match.apply(this,[this.textInput.val()]) }; func.ifNotText = function(num){ return num == 13 || num == 40 || num == 38 }; func.match = function(content){ func.removeCur.call(this).emailBox.show(); this.cur = null; var contentPrv = content.split(‘@‘)[0]; var contentNext = content.split(‘@‘)[1] || ‘‘; for(var i=0; i<this.links.length; i++){ this.emailList[i] = this.links[i].indexOf(contentNext)!=0 ? this.emailList[i].text(contentPrv+‘@‘+this.links[i]).hide() : this.emailList[i].text(contentPrv+‘@‘+this.links[i]).show(); } }; func.textBlur = function(){ func.clearTO.call(this); this.cur = null; func.removeCur.call(this); this.emailBox.hide(); return this }; func.removeCur = function(){ $.each(this.emailList,function(){ $(this).removeClass(‘cur‘) }); return this }; func.clearTO = function(){ clearTimeout(this.timeOut); this.timeOut = null; }; func.bindFocus = function(){ this.textInput.bind(‘keydown‘,func.keyDownFun.bind(this)).bind(‘keyup‘,func.keyUpFun.bind(this)); }; func.init.call(options); }})(jQuery);
html代碼:
<div class="inpt"> <div> <input type="text" class="email text" name="email" > <div class="textlist" style="display:none"> </div> </div> </div>
1. 建立ul,li,插入到emailBox中,同時給li綁定點擊事件(必須是mousedown事件,不能是click事件)為chooseLi方法. (查看→3)
因為點擊的時候元素就會失去焦點,觸發input的blur事件,但是必須讓li的點擊事件發生在input的blur事件之前,所以要用mousedown事件
2.
①. 給input的keydown事件綁定keyDownFun方法. (查看→5)
②. 給input的keyup事件綁定keyUpFun方法. (查看→6)
③. 給input的blur事件綁定textBlur方法. (查看→4)
3. chooseLi方法,選取點擊項填充input(點擊後也會觸發input的blur事件)
4. textBlur方法,清除timeOut,清除cur,刪除li的cur類名,隱藏聯想框
5. keyDownFun方法:
①清除timeout(防止輸入太快出現bug)
②如果按下的是斷行符號鍵,調用pressEnter方法,return (查看→7)
③如果按下的是‘下‘鍵,調用upAndDown方法,傳入true,return (查看→10)
④如果按下的是‘上‘鍵,調用upAndDown方法,傳入false,return (查看→10)
⑤設定timeOut為0秒後執行timeout方法,使輸入內容和聯想內容同步顯示 (查看→11)
6. keyUpFun方法:
①清除timeout(防止輸入太快出現bug)
②調用ifNotText方法,傳入e.keycode作為參數,判斷按下的是否是‘斷行符號,下,上‘,如果是的話,直接return
* 還是需要判斷輸入的是文字,還是‘上,下,斷行符號‘,如果是‘上,下,斷行符號‘,則不能直接再次匹配,因為匹配的過程就包括了重設cur.
③設定timeOut為0秒後執行timeout方法 (查看→11)
* 顯示聯想的內容在keydown和keyup時都要發生(如果只定義其中一個,輸入快了會有時不同步),而且即使發生兩次,也沒有問題. 但是斷行符號,上,下,這三個鍵的事件只能綁定keydown,因為它每次按鍵只能被調用一次.
7. pressEnter方法:
①如果當前沒有cur項,則調用noCurPressEnter方法 (查看→8)
②如果當前有cur項,則調用curPressEnter方法 (查看→9)
*判斷是否有cur項,不能使用 if(cur),或者if(!cur),因為cur的值有可能是0,是0的時候會得到和想要的值不同的結果
8. noCurPressEnter方法:
判斷輸入框裡是否有‘@‘符,並且@符不在最後一位,
①已經有@符並且不在最後一位,則表示已輸入想要的結果,直接觸發input的blur事件.
②輸入的內容裡沒有@符,或者@符後面沒有內容,則表示沒有輸入完整郵箱,將第一個li裡的內容填充到input,再觸發input的blur事件
9. curPressEnter方法:
直接將cur項的值填充到input,然後input失去焦點
10. upAndDown方法:
①定義length屬性為當前顯示的li項的數量
②判斷當前是否有cur項,有的話將cur轉到下一個或上一個(根據傳入的參數判斷是上還是下)
③沒有的話將cur轉到第一個或者最後一個(根據傳入的參數判斷是上還是下)
11. timeout方法:
①如果input框內容為空白,隱藏emailBox
②如果input框內容不為空白,調用match方法 (查看→13)
12. ifNotText方法:
判斷是否是‘斷行符號,下,上‘鍵,是其中之一的話,返回true
13. match方法:
①清空cur項
*如果已經通過鍵盤選擇了一個郵箱,又接著輸入內容,則原來被選中的項必須清空. 如果不清空,新輸入的內容會導致篩選條件的變化,導致顯示的項也發生變化,這時假設接著按‘下‘,則cur的改變不是新篩選出的顯示項目,依然是原來的顯示項目,導致錯誤.
②讓emailBox顯示
③按照@符分割輸入的內容
④每個li的內容應該就是@前面的內容+links的內容
⑤讓@後面的內容去匹配每個links的開頭,匹配成功的顯示,匹配失敗的隱藏
github地址: https://github.com/OOP-Code-Bunny/ocj-index-v3.0.0/blob/master/common/js/common/jquery.autoComplate.js