JS實(shí)現(xiàn)的一個(gè)簡(jiǎn)單的Autocomplete自動(dòng)完成例子
分享一篇無(wú)意間發(fā)現(xiàn)的自動(dòng)完成源碼。這里測(cè)試的時(shí)候使用的是數(shù)組,實(shí)際使用的時(shí)候,我們換成Ajax從服務(wù)器端獲取的方式就OK了。
提示:可以直接保存到一個(gè)html文件中查看效果。
<!doctype html>
<html>
<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>
<h1 align="center">自動(dòng)完成函數(shù)(Autocomplete Function)</h1>
<div align="center"><input type="text" style="width:300px;height:20px;font-size:14pt;" 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>
- Autocomplete Textbox Example javascript實(shí)現(xiàn)自動(dòng)完成成功
- JS 自動(dòng)完成 AutoComplete(Ajax 查詢)
- jquery自動(dòng)完成插件(autocomplete)應(yīng)用之PHP版
- Jquery AutoComplete自動(dòng)完成 的使用方法實(shí)例
- jQuery.Autocomplete實(shí)現(xiàn)自動(dòng)完成功能(詳解)
- jQuery Autocomplete自動(dòng)完成插件
- jquery autocomplete自動(dòng)完成插件的的使用方法
- jQuery UI AutoComplete 自動(dòng)完成使用小記
- jQuery 插件autocomplete自動(dòng)完成應(yīng)用(自動(dòng)補(bǔ)全)(asp.net后臺(tái))
- 基于bootstrap插件實(shí)現(xiàn)autocomplete自動(dòng)完成表單
相關(guān)文章
使用webpack搭建pixi.js開發(fā)環(huán)境
這篇文章主要介紹了使用webpack搭建pixi.js開發(fā)環(huán)境,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
JavaScript實(shí)現(xiàn)簡(jiǎn)單評(píng)論功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單評(píng)論功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
uniapp video播放視頻 懸浮在屏幕無(wú)法滑動(dòng)
在uniapp中,需要使用<video></video>標(biāo)簽進(jìn)行播放動(dòng)態(tài)src的視頻,這里只是簡(jiǎn)單的在App端播放視頻,且動(dòng)態(tài)賦值src,如果還有其它復(fù)雜的布局內(nèi)部嵌套video標(biāo)簽也是不成功的,例如:<swiper>、<scroll-view>等,感興趣的朋友跟隨小編一起看看吧2024-08-08
AJAX跨域請(qǐng)求json數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章介紹了AJAX跨域請(qǐng)求json數(shù)據(jù)的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-11-11
js實(shí)現(xiàn)統(tǒng)計(jì)字符串中特定字符出現(xiàn)個(gè)數(shù)的方法
這篇文章主要介紹了js實(shí)現(xiàn)統(tǒng)計(jì)字符串中特定字符出現(xiàn)個(gè)數(shù)的方法,涉及javascript針對(duì)字符串中字符運(yùn)算操作相關(guān)技巧,需要的朋友可以參考下2016-08-08
JS關(guān)閉窗口時(shí)產(chǎn)生的事件及用法示例
這篇文章主要介紹了JS關(guān)閉窗口時(shí)產(chǎn)生的事件及用法,結(jié)合關(guān)閉窗口時(shí)提交評(píng)論的實(shí)例分析了javascript事件觸發(fā)機(jī)制及遮罩層實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
JavaScript基于inquirer封裝一個(gè)控制臺(tái)文件選擇器
這篇文章主要介紹了JavaScript基于inquirer封裝一個(gè)控制臺(tái)文件選擇器,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08

