使用jquery實現(xiàn)仿百度自動補全特效
更新時間:2015年07月23日 11:25:10 投稿:hebedich
這里給大家分享的效果是像百度的搜索框一樣,當用戶在文本框輸入前幾個字母或是漢字的時候,該控件就能從存放數(shù)據(jù)的文或是數(shù)據(jù)庫里將所有以這些字母開頭的數(shù)據(jù)提示給用戶,供用戶選擇,提供方便,增加用戶體驗。
新建index.html文件,直接復制下面代碼到新建的文件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é)點
this.value_arr=arr; //不要包含重復值
this.index=-1; //當前選中的DIV的索引
this.search_value=""; //保存當前搜索的字符
}
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";
}
},
//模擬鼠標移動至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;
//光標鍵"↓"
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);
}
//光標鍵"↑"
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實現(xiàn)郵箱下拉列表自動補全功能
- jQuery實現(xiàn)文本框郵箱輸入自動補全效果
- jQuery實現(xiàn)Email郵箱地址自動補全功能代碼
- jquery 實現(xiàn)輸入郵箱時自動補全下拉提示功能
- jquery實現(xiàn)郵箱自動補全功能示例分享
- PHP+jQuery實現(xiàn)自動補全功能源碼
- jQuery 插件autocomplete自動完成應(yīng)用(自動補全)(asp.net后臺)
- jquery實現(xiàn)翻動fadeIn顯示的方法
- jQuery實現(xiàn)Flash效果上下翻動的中英文導航菜單代碼
- jQuery仿Flash上下翻動的中英文導航菜單實例
- jQuery實現(xiàn)輸入框郵箱內(nèi)容自動補全與上下翻動顯示效果【附demo源碼下載】
相關(guān)文章
jQuery+ajax實現(xiàn)動態(tài)添加表格tr td功能示例
這篇文章主要介紹了jQuery+ajax實現(xiàn)動態(tài)添加表格tr td功能,結(jié)合實例形式分析了jQuery基于ajax動態(tài)創(chuàng)建頁面table元素相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
jQuery實現(xiàn)高度靈活的表單驗證功能示例【無UI】
這篇文章主要介紹了jQuery實現(xiàn)高度靈活的表單驗證功能,涉及jQuery正則判斷與頁面元素動態(tài)操作相關(guān)使用技巧,需要的朋友可以參考下2020-04-04
Mui使用jquery并且使用點擊跳轉(zhuǎn)新窗口的實例
下面小編就為大家?guī)硪黄狹ui使用jquery并且使用點擊跳轉(zhuǎn)新窗口的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
jQuery 獲取、設(shè)置HTML或TEXT內(nèi)容的兩種方法
這篇文章主要介紹了jQuery 獲取、設(shè)置HTML或TEXT內(nèi)容的兩種方法,需要的朋友可以參考下2014-05-05
基于jQuery.validate及Bootstrap的tooltip開發(fā)氣泡樣式的表單校驗組件思路詳解
這篇文章主要介紹了基于jQuery.validate及Bootstrap的tooltip開發(fā)氣泡樣式的表單校驗組件的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧2016-07-07

