邮箱js自动补全功能

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>inputSuggest输入字符时提示</title> 
<style type="text/css"> 
body{margin:0;padding:0;} 
input{width:200px;} 
.suggest-container{border:1px solid #C1C1C1;visibility:hidden;} 
.suggest-item{padding:3px 2px; font-size:16px; color:#0099CC;} 
.suggest-active {background:#33CCFF;color:white;padding:3px 2px;font-size:16px;font-family:"黑体";} 
</style> 
<script type="text/javascript"> 
function InputSuggest(opt){ 
this.win = null; 
this.doc = null; 
this.container = null; 
this.items = null; 
this.input = opt.input || null; 
this.containerCls = opt.containerCls || 'suggest-container'; 
this.itemCls = opt.itemCls || 'suggest-item'; 
this.activeCls = opt.activeCls || 'suggest-active'; 
this.width = opt.width; 
this.opacity = opt.opacity; 
this.data = opt.data || []; 
this.active = null; 
this.visible = false; 
this.init(); 
} 
InputSuggest.prototype = { 
init: function(){ 
this.win = window; 
this.doc = window.document; 
this.container = this.$C('div'); 
this.attr(this.container, 'class', this.containerCls); 
this.doc.body.appendChild(this.container); 
this.setPos(); 
var _this = this, input = this.input; 
this.on(input,'keyup',function(e){ 
if(input.value==''){ 
_this.hide(); 
}else{ 
_this.onKeyup(e); 
} 
}); 
// blur会在click前发生,这里使用mousedown 
this.on(input,'blur',function(e){ 
_this.hide(); 
}); 
this.onMouseover(); 
this.onMousedown(); 
}, 
$C: function(tag){ 
return this.doc.createElement(tag); 
}, 
getPos: function (el){ 
var pos=[0,0], a=el; 
if(el.getBoundingClientRect){ 
var box = el.getBoundingClientRect(); 
pos=[box.left,box.top]; 
}else{ 
while(a && a.offsetParent){ 
pos[0] += a.offsetLeft; 
pos[1] += a.offsetTop; 
a = a.offsetParent 
} 
} 
return pos; 
}, 
setPos: function(){ 
var input = this.input, 
pos = this.getPos(input), 
brow = this.brow, 
width = this.width, 
opacity = this.opacity, 
container = this.container; 
container.style.cssText = 
'position:absolute;overflow:hidden;left:' 
+ pos[0] + 'px;top:' 
+ (pos[1]+input.offsetHeight) + 'px;width:' 
// IE6/7/8/9/Chrome/Safari input[type=text] border默认为2,Firefox为1,因此取offsetWidth-2保证与FF一致 
+ (brow.firefox ? input.clientWidth : input.offsetWidth-2) + 'px;'; 
if(width){ 
container.style.width = width + 'px'; 
} 
if(opacity){ 
if(this.brow.ie){ 
container.style.filter = 'Alpha(Opacity=' + opacity * 100 + ');'; 
}else{ 
container.style.opacity = (opacity == 1 ? '' : '' + opacity); 
} 
} 
}, 
show: function(){ 
this.container.style.visibility = 'visible'; 
this.visible = true; 
}, 
hide: function(){ 
this.container.style.visibility = 'hidden'; 
this.visible = false; 
}, 
attr: function(el, name, val){ 
if(val === undefined){ 
return el.getAttribute(name); 
}else{ 
el.setAttribute(name,val); 
name=='class' && (el.className = val); 
} 
}, 
on: function(el, type, fn){ 
el.addEventListener ? el.addEventListener(type, fn, false) : el.attachEvent('on' + type, fn); 
}, 
un: function(el, type, fn){ 
el.removeEventListener ? el.removeEventListener(type, fn, false) : el.detachEvent('on' + type, fn); 
}, 
brow: function(ua){ 
return { 
ie: /msie/.test(ua) && !/opera/.test(ua), 
opera: /opera/.test(ua), 
firefox: /firefox/.test(ua) 
}; 
}(navigator.userAgent.toLowerCase()), 
onKeyup: function(e){ 
var container = this.container, input = this.input, iCls = this.itemCls, aCls = this.activeCls; 
if(this.visible){ 
switch(e.keyCode){ 
case 13: // Enter 
if(this.active){ 
input.value = this.active.firstChild.data; 
this.hide(); 
} 
return; 
case 38: // 方向键上 
if(this.active==null){ 
this.active = container.lastChild; 
this.attr(this.active, 'class', aCls); 
input.value = this.active.firstChild.data; 
}else{ 
if(this.active.previousSibling!=null){ 
this.attr(this.active, 'class', iCls); 
this.active = this.active.previousSibling; 
this.attr(this.active, 'class', aCls); 
input.value = this.active.firstChild.data; 
}else{ 
this.attr(this.active, 'class', iCls); 
this.active = null; 
input.focus(); 
input.value = input.getAttribute("curr_val"); 
} 
} 
return; 
case 40: // 方向键下 
if(this.active==null){ 
this.active = container.firstChild; 
this.attr(this.active, 'class', aCls); 
input.value = this.active.firstChild.data; 
}else{ 
if(this.active.nextSibling!=null){ 
this.attr(this.active, 'class', iCls); 
this.active = this.active.nextSibling; 
this.attr(this.active, 'class', aCls); 
input.value = this.active.firstChild.data; 
}else{ 
this.attr(this.active, 'class', iCls); 
this.active = null; 
input.focus(); 
input.value = input.getAttribute("curr_val"); 
} 
} 
return; 
} 
// 烈火網 liehuo.net 欢迎复制,拒绝恶意采集 veryhuo.COm 
} 
if(e.keyCode==27){ // ESC键 
this.hide(); 
input.value = this.attr(input,'curr_val'); 
return; 
} 
if(input.value.indexOf('@')!=-1){return;} 
this.items = []; 
if(this.attr(input,'curr_val')!=input.value){ 
this.container.innerHTML = ''; 
for(var i=0,len=this.data.length;i<len;i++){ 
var item = this.$C('div'); 
this.attr(item, 'class', this.itemCls); 
item.innerHTML = input.value + '@' + this.data[i]; 
this.items[i] = item; 
this.container.appendChild(item); 
} 
this.attr(input,'curr_val',input.value); 
} 
this.show(); 
}, 
onMouseover: function(){ 
var _this = this, icls = this.itemCls, acls = this.activeCls; 
this.on(this.container,'mouseover',function(e){ 
var target = e.target || e.srcElement; 
if(target.className == icls){ 
if(_this.active){ 
_this.active.className = icls; 
} 
target.className = acls; 
_this.active = target; 
} 
}); 
}, 
onMousedown: function(){ 
var _this = this; 
this.on(this.container,'mousedown',function(e){ 
var target = e.target || e.srcElement; 
_this.input.value = target.innerHTML; 
_this.hide(); 
}); 
} 
} 
</script> 
<script type="text/javascript"> 
window.onload = function(){ 
var sinaSuggest = new InputSuggest({ 
input: document.getElementById('sina'), 
data: ['qq.com','163.com','126.com','sina.com','vip.sina.com','hotmail.com','gmail.com','sina.cn','suho.com','139.com','wo.com.cn','189.cn'] 
}); 

} 
</script> 
</head> 
<body> 
<div style="width:400px;margin:30px auto;text-align:center;"> 
<label>新浪</label> 
<input type="text" id="sina"/> 
</div> 
</body> 
</html> 

inputSuggest输入字符时提示

新浪

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值