使用jquery實(shí)現(xiàn)仿百度自動(dòng)補(bǔ)全特效
新建index.html文件,直接復(fù)制下面代碼到新建的文件index.html里面,用瀏覽器訪問(wèn),僅用于參考:
<!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è)置對(duì)象的層疊順序 */
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";//減去邊框的長(zhǎng)度2px
},
//刪除自動(dòng)完成需要的所有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)移動(dòng)至DIV時(shí),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="請(qǐng)輸入a或b模擬效果" id="o" onkeyup="autoComplete.start(event)">
</div>
<div class="auto_hidden" id="auto"><!--自動(dòng)完成 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>
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- jQuery實(shí)現(xiàn)郵箱下拉列表自動(dòng)補(bǔ)全功能
- jQuery實(shí)現(xiàn)文本框郵箱輸入自動(dòng)補(bǔ)全效果
- jQuery實(shí)現(xiàn)Email郵箱地址自動(dòng)補(bǔ)全功能代碼
- jquery 實(shí)現(xiàn)輸入郵箱時(shí)自動(dòng)補(bǔ)全下拉提示功能
- jquery實(shí)現(xiàn)郵箱自動(dòng)補(bǔ)全功能示例分享
- PHP+jQuery實(shí)現(xiàn)自動(dòng)補(bǔ)全功能源碼
- jQuery 插件autocomplete自動(dòng)完成應(yīng)用(自動(dòng)補(bǔ)全)(asp.net后臺(tái))
- jquery實(shí)現(xiàn)翻動(dòng)fadeIn顯示的方法
- jQuery實(shí)現(xiàn)Flash效果上下翻動(dòng)的中英文導(dǎo)航菜單代碼
- jQuery仿Flash上下翻動(dòng)的中英文導(dǎo)航菜單實(shí)例
- jQuery實(shí)現(xiàn)輸入框郵箱內(nèi)容自動(dòng)補(bǔ)全與上下翻動(dòng)顯示效果【附demo源碼下載】
相關(guān)文章
jquery獲取ASP.NET服務(wù)器端控件dropdownlist和radiobuttonlist生成客戶端HTML標(biāo)簽
jquery獲取ASP.NET服務(wù)器端控件dropdownlist和radiobuttonlist生成客戶端HTML標(biāo)簽后的value和text值2010-06-06
jQuery+jRange實(shí)現(xiàn)滑動(dòng)選取數(shù)值范圍特效
本文向大家介紹一款范圍選擇器插件jRange,它是基于jQuery的一款簡(jiǎn)單插件。本站之前有類似文章:jQuery+CSS使用滑塊選取價(jià)格范圍,其中不太好的地方是使用了jqueryui龐大的插件庫(kù),有興趣的朋友可以看看。下面我們來(lái)看下插件jRange的使用。2015-03-03
jquery實(shí)現(xiàn)textarea輸入字符控制(仿微博輸入控制字符)
首先獲取輸入框的值然后獲取輸入框的值;如果字?jǐn)?shù)大于500、發(fā)布按鈕禁用否從發(fā)布按鈕啟用,感興趣的朋友可以詳細(xì)了解下哈2013-04-04
jQuery Easyui datagrid連續(xù)發(fā)送兩次請(qǐng)求問(wèn)題
用上述方式動(dòng)態(tài)加載datagrid的數(shù)據(jù)時(shí),通過(guò)net監(jiān)聽(tīng),發(fā)現(xiàn)調(diào)用了兩遍XX方法,目前的解決方案是,將url放到datagrid初始化的時(shí)候執(zhí)行2016-12-12
基于jquery的動(dòng)態(tài)創(chuàng)建表格的插件
工作快一年了,最近學(xué)習(xí)jquery,對(duì)jquery很感興趣。第一次寫博客,有不足之處還請(qǐng)各位拍磚。2011-04-04
jquery實(shí)現(xiàn)ajax提交form表單的方法總結(jié)
本篇文章主要是對(duì)jquery實(shí)現(xiàn)ajax提交form表單的方法進(jìn)行了總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-03-03
JQuery給元素添加/刪除節(jié)點(diǎn)比如select
本教程詳細(xì)介紹下jQuery添加/刪除Select的Option項(xiàng),感興趣的各位可以參考下哈2013-04-04
jQuery+CSS實(shí)現(xiàn)菜單滑動(dòng)伸展收縮(仿淘寶)
淘寶網(wǎng)上的菜單滑動(dòng)伸展收縮想必大家不會(huì)很陌生吧,接下來(lái)本文也使用jquery+css實(shí)現(xiàn)一個(gè),代碼很簡(jiǎn)潔功能很使用,感興趣的各位可以不要錯(cuò)過(guò)了哈2013-03-03
jQuery獲取select選中的option的value值實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇jQuery獲取select選中的option的value值實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08

