JS實現(xiàn)的一個簡單的Autocomplete自動完成例子
分享一篇無意間發(fā)現(xiàn)的自動完成源碼。這里測試的時候使用的是數(shù)組,實際使用的時候,我們換成Ajax從服務(wù)器端獲取的方式就OK了。
提示:可以直接保存到一個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è)置對象的層疊順序 */
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; //不要包含重復(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>
<h1 align="center">自動完成函數(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"><!--自動完成 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實現(xiàn)自動完成成功
- JS 自動完成 AutoComplete(Ajax 查詢)
- jquery自動完成插件(autocomplete)應(yīng)用之PHP版
- Jquery AutoComplete自動完成 的使用方法實例
- jQuery.Autocomplete實現(xiàn)自動完成功能(詳解)
- jQuery Autocomplete自動完成插件
- jquery autocomplete自動完成插件的的使用方法
- jQuery UI AutoComplete 自動完成使用小記
- jQuery 插件autocomplete自動完成應(yīng)用(自動補全)(asp.net后臺)
- 基于bootstrap插件實現(xiàn)autocomplete自動完成表單
相關(guān)文章
JS+ACTIVEX實現(xiàn)網(wǎng)頁選擇本地目錄路徑對話框
使用ACTIVEX對象遍歷本地磁盤和目錄,只需網(wǎng)頁啟用相關(guān)ACTIVEX的INTERNET選項即可實現(xiàn)路徑選擇對話框,感興趣的你可以參考下哈希望可以幫助到你2013-03-03Javascript中獲取出錯代碼所在文件及行數(shù)的代碼
之前在做一個Javascript的日志控制臺功能模塊,希望能夠在Javascript代碼出錯時捕獲此錯誤,并將出錯的文件及相應(yīng)的行數(shù)打印到控制臺并匯報給服務(wù)器。2010-09-09toString.call()通用的判斷數(shù)據(jù)類型方法示例
這篇文章主要給大家介紹了關(guān)于toString.call()通用的判斷數(shù)據(jù)類型方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08innerHTML在Mozilla Firefox和Opera下執(zhí)行的一個特例情況。
innerHTML在Mozilla Firefox和Opera下執(zhí)行的一個特例情況。...2007-01-01TypeScript 獲取函數(shù)的參數(shù)類型、返回值類型及定義返回函數(shù)類型
這篇文章主要介紹了TypeScript 獲取函數(shù)的參數(shù)類型、返回值類型及定義返回函數(shù)類型,需要的朋友可以參考下2024-02-02javascript eval()應(yīng)用實例 select
javascript eval應(yīng)用小例子。實例代碼就是控制checkbox的選擇與取消的函數(shù),非常不錯。2009-07-07