用js實現(xiàn)輸入提示(自動完成)的實例代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>autoComplete</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.autoComplete {margin:8px;position:relative;float:left;}
.autoComplete input {width:200px;height:25px;margin:0;padding:0;line-height:25px;}
.autoComplete ul {z-index:-12;padding:0px;margin:0px;border:1px #333 solid;width:200px;background:white;display:none;position:absolute;left:0;top:28px;*margin-left:9px;*margin-top:2px;margin-top:1px;}
.autoComplete li {list-style:none;}
.autoComplete li a {display:block;color:#000;text-decoration:none;padding:1px 0 1px 5px;_width:97%;}
.autoComplete li a:hover {color:#000;background:#ccc;border:none;}
</style>
<script type="text/javascript">
//<![CDATA[
var getElementsByClassName = function (searchClass, node, tag) {/* 兼容各瀏覽器的選擇class的方法;(:http://www.dbjr.com.cn,想了解更多請看這個地址) */
node = node || document, tag = tag ? tag.toUpperCase() : "*";
if(document.getElementsByClassName){/* 支持getElementsByClassName的瀏覽器 */
var temp = node.getElementsByClassName(searchClass);
if(tag=="*"){
return temp;
} else {
var ret = new Array();
for(var i=0; i<temp.length; i++)
if(temp[i].nodeName==tag)
ret.push(temp[i]);
return ret;
}
}else{/* 不支持getElementsByClassName的瀏覽器 */
var classes = searchClass.split(" "),
elements = (tag === "*" && node.all)? node.all : node.getElementsByTagName(tag),
patterns = [], returnElements = [], current, match;
var i = classes.length;
while(--i >= 0)
patterns.push(new RegExp("(^|s)" + classes[i] + "(s|$)"));
var j = elements.length;
while(--j >= 0){
current = elements[j], match = false;
for(var k=0, kl=patterns.length; k<kl; k++){
match = patterns[k].test(current.className);
if(!match) break;
}
if(match) returnElements.push(current);
}
return returnElements;
}
};
var addEvent=(function(){/* 用此函數(shù)添加事件防止事件覆蓋 */
if(document.addEventListener){
return function(type, fn){ this.addEventListener(type, fn, false); };
}else if(document.attachEvent){
return function(type,fn){
this.attachEvent(on+type, function () {
return fn.call(this, window.event);/* 兼容IE */
});
};
}
})();
;(function(window){
/* 插件開始 */
var autoComplete=function(o){
var handler=(function(){
var handler=function(e,o){ return new handler.prototype.init(e,o); };/* 為每個選擇的dom都創(chuàng)建一個相對應(yīng)的對象,這樣選擇多個dom時可以很方便地使用 */
handler.prototype={
e:null, o:null, timer:null, show:0, input:null, popup:null,
init:function(e,o){/* 設(shè)置初始對象 */
this.e=e, this.o=o,
this.input=this.e.getElementsByTagName(this.o.input)[0],
this.popup=this.e.getElementsByTagName(this.o.popup)[0],
this.initEvent();/* 初始化各種事件 */
},
match:function(quickExpr,value,source){/* 生成提示 */
var li = null;
for(var i in source){
if( value.length>0 && quickExpr.exec(source[i])!=null ){
li = document.createElement(li);
li.innerHTML = <a href="javascript:;">+source[i]+</a>;
this.popup.appendChild(li);
}
}
if(this.popup.getElementsByTagName(a).length)
this.popup.style.display=block;
else
this.popup.style.display=none;
},
&n
相關(guān)文章
javascript十六進(jìn)制數(shù)字和ASCII字符之間的轉(zhuǎn)換方法
下面小編就為大家?guī)硪黄猨avascript十六進(jìn)制數(shù)字和ASCII字符之間的轉(zhuǎn)換方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12JavaScript實現(xiàn)視頻轉(zhuǎn)GIF的示例代碼
這篇文章主要介紹了JavaScript實現(xiàn)視頻轉(zhuǎn)GIF,本文一共會按照以下三步去實現(xiàn)一個視頻轉(zhuǎn)?GIF?功能,解封裝視頻,從視頻文件中獲取視頻幀,解碼視頻幀,獲取幀圖像信息,拼裝幀圖像信息,生成?GIF,需要的朋友可以參考下2024-03-03JavaScript之排序函數(shù)_動力節(jié)點(diǎn)Java學(xué)院整理
排序也是在程序中經(jīng)常用到的算法。這篇文章主要介紹了JavaScript之排序函數(shù),有興趣的可以了解一下2017-06-06js獲取鼠標(biāo)點(diǎn)擊的位置實現(xiàn)思路及代碼
常用的是 event.clientX和event.clientY分別獲取橫向的和縱向的位置,但僅使用這個方法是不夠的,感興趣的朋友可以了解本文2014-05-05javascript eval函數(shù)深入認(rèn)識
發(fā)現(xiàn)為本文起一個合適的標(biāo)題還不是那么容易,呵呵,所以在此先說明下本文的兩個目的2009-02-02阻止事件(取消瀏覽器對事件的默認(rèn)行為并阻止其傳播)
取消瀏覽器對事件的默認(rèn)行為(響應(yīng))(比如a標(biāo)簽的跳轉(zhuǎn)等)并停止事件的繼續(xù)傳播,下面有一個不錯的示例大家可以感受下2013-11-11JavaScript中數(shù)組遍歷的7種方法小結(jié)
作為JavaScript開發(fā)人員,熟悉數(shù)組的遍歷和操作是非常重要的,數(shù)組遍歷是處理和操作數(shù)組元素的基本需求之一,本文將介紹JavaScript中的7種常見數(shù)組遍歷方法,幫助你成為數(shù)組操作的達(dá)人2023-11-11JavaScript本地數(shù)據(jù)存儲sessionStorage與localStorage使用詳解
這篇文章主要介紹了JavaScript本地數(shù)據(jù)存儲sessionStorage與localStorage使用,localStorage:永久存儲在本地,適合保存在本地的數(shù)據(jù)。sessionStorage:會話級的存儲,敏感帳號一次登陸2022-10-10