使用jquery實(shí)現(xiàn)仿百度自動補(bǔ)全特效
新建index.html文件,直接復(fù)制下面代碼到新建的文件index.html里面,用瀏覽器訪問,僅用于參考:
<!doctype html> <html> <meta charset="utf-8"> <style> body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; } .auto_hidden { width:204px;border-top: 1px solid #333; border-bottom: 1px solid #333; border-left: 1px solid #333; border-right: 1px solid #333; position:absolute; display:none; } .auto_show { width:204px; border-top: 1px solid #333; border-bottom: 1px solid #333; border-left: 1px solid #333; border-right: 1px solid #333; position:absolute; z-index:9999; /* 設(shè)置對象的層疊順序 */ display:block; } .auto_onmouseover{ color:#ffffff; background-color:highlight; width:100%; } .auto_onmouseout{ color:#000000; width:100%; background-color:#ffffff; } </style> <script language="javascript"> <!-- var $ = 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,autoObj,arr){ this.obj=$(obj); //輸入框 this.autoObj=$(autoObj);//DIV的根節(jié)點(diǎn) this.value_arr=arr; //不要包含重復(fù)值 this.index=-1; //當(dāng)前選中的DIV的索引 this.search_value=""; //保存當(dāng)前搜索的字符 } AutoComplete.prototype={ //初始化DIV的位置 init: function(){ 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 - 2 + "px";//減去邊框的長度2px }, //刪除自動完成需要的所有DIV deleteDIV: function(){ while(this.autoObj.hasChildNodes()){ this.autoObj.removeChild(this.autoObj.firstChild); } this.autoObj.className="auto_hidden"; }, //設(shè)置值 setValue: function(_this){ return function(){ _this.obj.value=this.seq; _this.autoObj.className="auto_hidden"; } }, //模擬鼠標(biāo)移動至DIV時,DIV高亮 autoOnmouseover: function(_this,_div_index){ return function(){ _this.index=_div_index; var length = _this.autoObj.children.length; for(var j=0;j<length;j++){ if(j!=_this.index ){ _this.autoObj.childNodes[j].className='auto_onmouseout'; }else{ _this.autoObj.childNodes[j].className='auto_onmouseover'; } } } }, //更改classname changeClassname: function(length){ for(var i=0;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; } } } , //響應(yīng)鍵盤 pressKey: function(event){ var length = this.autoObj.children.length; //光標(biāo)鍵"↓" if(event.keyCode==40){ ++this.index; if(this.index>length){ this.index=0; }else if(this.index==length){ this.obj.value=this.search_value; } this.changeClassname(length); } //光標(biāo)鍵"↑" else if(event.keyCode==38){ this.index--; if(this.index<-1){ this.index=length - 1; }else if(this.index==-1){ this.obj.value=this.search_value; } this.changeClassname(length); } //回車鍵 else if(event.keyCode==13){ this.autoObj.className="auto_hidden"; this.index=-1; }else{ this.index=-1; } }, //程序入口 start: function(event){ if(event.keyCode!=13&&event.keyCode!=38&&event.keyCode!=40){ this.init(); this.deleteDIV(); this.search_value=this.obj.value; var valueArr=this.value_arr; valueArr.sort(); if(this.obj.value.replace(/(^\s*)|(\s*$)/g,'')==""){ return; }//值為空,退出 try{ var reg = new RegExp("(" + this.obj.value + ")","i");} catch (e){ return; } var div_index=0;//記錄創(chuàng)建的DIV的索引 for(var i=0;i<valueArr.length;i++){ if(reg.test(valueArr[i])){ var div = document.createElement("div"); div.className="auto_onmouseout"; div.seq=valueArr[i]; div.onclick=this.setValue(this); div.onmouseover=this.autoOnmouseover(this,div_index); div.innerHTML=valueArr[i].replace(reg,"<strong>$1</strong>");//搜索到的字符粗體顯示 this.autoObj.appendChild(div); this.autoObj.className="auto_show"; div_index++; } } } this.pressKey(event); window.onresize=Bind(this,function(){this.init();}); } } //--> </script> <body> <div align="center" style="padding-top:50px"> <input type="text" style="width:300px;height:20px;font-size:14pt;" placeholder="請輸入a或b模擬效果" id="o" onkeyup="autoComplete.start(event)"> </div> <div class="auto_hidden" id="auto"><!--自動完成 DIV--></div> <script> 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']); </script> </body> </html>
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
- jQuery實(shí)現(xiàn)郵箱下拉列表自動補(bǔ)全功能
- jQuery實(shí)現(xiàn)文本框郵箱輸入自動補(bǔ)全效果
- jQuery實(shí)現(xiàn)Email郵箱地址自動補(bǔ)全功能代碼
- jquery 實(shí)現(xiàn)輸入郵箱時自動補(bǔ)全下拉提示功能
- jquery實(shí)現(xiàn)郵箱自動補(bǔ)全功能示例分享
- PHP+jQuery實(shí)現(xiàn)自動補(bǔ)全功能源碼
- jQuery 插件autocomplete自動完成應(yīng)用(自動補(bǔ)全)(asp.net后臺)
- jquery實(shí)現(xiàn)翻動fadeIn顯示的方法
- jQuery實(shí)現(xiàn)Flash效果上下翻動的中英文導(dǎo)航菜單代碼
- jQuery仿Flash上下翻動的中英文導(dǎo)航菜單實(shí)例
- jQuery實(shí)現(xiàn)輸入框郵箱內(nèi)容自動補(bǔ)全與上下翻動顯示效果【附demo源碼下載】
相關(guān)文章
jQuery+CSS實(shí)現(xiàn)簡單切換菜單示例
這篇文章主要介紹了jQuery+CSS實(shí)現(xiàn)簡單切換菜單的方法,涉及jQuery針對頁面元素的遍歷及樣式動態(tài)變換相關(guān)技巧,需要的朋友可以參考下2016-07-07jQuery+ajax實(shí)現(xiàn)動態(tài)添加表格tr td功能示例
這篇文章主要介紹了jQuery+ajax實(shí)現(xiàn)動態(tài)添加表格tr td功能,結(jié)合實(shí)例形式分析了jQuery基于ajax動態(tài)創(chuàng)建頁面table元素相關(guān)操作技巧,需要的朋友可以參考下2018-04-04jQuery實(shí)現(xiàn)高度靈活的表單驗(yàn)證功能示例【無UI】
這篇文章主要介紹了jQuery實(shí)現(xiàn)高度靈活的表單驗(yàn)證功能,涉及jQuery正則判斷與頁面元素動態(tài)操作相關(guān)使用技巧,需要的朋友可以參考下2020-04-04Mui使用jquery并且使用點(diǎn)擊跳轉(zhuǎn)新窗口的實(shí)例
下面小編就為大家?guī)硪黄狹ui使用jquery并且使用點(diǎn)擊跳轉(zhuǎn)新窗口的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08jQuery 獲取、設(shè)置HTML或TEXT內(nèi)容的兩種方法
這篇文章主要介紹了jQuery 獲取、設(shè)置HTML或TEXT內(nèi)容的兩種方法,需要的朋友可以參考下2014-05-05jquery圖片預(yù)覽插件實(shí)現(xiàn)方法詳解
這篇文章主要為大家詳細(xì)介紹了jquery圖片預(yù)覽插件的實(shí)現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07基于jQuery.validate及Bootstrap的tooltip開發(fā)氣泡樣式的表單校驗(yàn)組件思路詳解
這篇文章主要介紹了基于jQuery.validate及Bootstrap的tooltip開發(fā)氣泡樣式的表單校驗(yàn)組件的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧2016-07-07