jQuery實(shí)現(xiàn)Email郵箱地址自動(dòng)補(bǔ)全功能代碼
本文實(shí)例講述了jQuery實(shí)現(xiàn)Email郵箱地址自動(dòng)補(bǔ)全功能代碼。分享給大家供大家參考,具體如下:
jQuery Email郵箱地址自動(dòng)補(bǔ)全代碼,輸入Email時(shí),會(huì)自動(dòng)加入@符號(hào),在輸入框中輸入“qq”、“Sina”、“163”等等可以看到效果;鼠標(biāo)經(jīng)過提示Email時(shí),高亮該條Email,鼠標(biāo)點(diǎn)擊Email時(shí),文本框內(nèi)容替換成該條Email,并刪除提示層.
運(yùn)行效果截圖如下:

在線演示地址如下:
http://demo.jb51.net/js/2015/jquery-email-auto-comp-codes/
具體代碼如下:
<!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>輸入Email相關(guān)字符自動(dòng)提示Email地址</title>
<script src="jquery-1.6.2.min.js" type="text/javascript"></script>
<style type="text/css">
body
{
margin:0px;
padding:0px;
font-family:Arial;
font-size:12px;
padding:10px;
}
#myemail, .newemail, .newemailtitle{
cursor:default;
line-height:18px;
}
</style>
</head>
<body>
Email <input id="me" type="text" value="" style="width:150px; height:18px; line-height:18px; border:1px solid #999;">
<script type="text/javascript">
var nowid;
var totalid;
var can1press = false;
var emailafter;
var emailbefor;
$(document).ready(function(){
$("#me").focus(function(){ //文本框獲得焦點(diǎn),插入Email提示層
$("#myemail").remove();
$(this).after("<div id='myemail' style='width:170px; height:auto; background:#fff; color:#6B6B6B; position:absolute; left:"+$(this).get(0).offsetLeft+"px; top:"+($(this).get(0).offsetTop+$(this).height()+2)+"px; border:1px solid #ccc;z-index:5px; '></div>");
if($("#myemail").html()){
$("#myemail").css("display","block");
$(".newemail").css("width",$("#myemail").width());
can1press = true;
} else {
$("#myemail").css("display","none");
can1press = false;
}
}).keyup(function(){ //文本框輸入文字時(shí),顯示Email提示層和常用Email
var press = $("#me").val();
if (press!="" || press!=null){
var emailtxt = "";
var emailvar = new Array("@163.com","@126.com","@yahoo.com","@qq.com","@sina.com","@gmail.com","@hotmail.com","@foxmail.com");
totalid = emailvar.length;
var emailmy = "<div class='newemail' style='width:170px; color:#6B6B6B; overflow:hidden;'><font color='#D33022'>" + press + "</font></div>";
if(!(isEmail(press))){
for(var i=0; i<emailvar.length; i++) {
emailtxt = emailtxt + "<div class='newemail' style='width:170px; color:#6B6B6B; overflow:hidden;'><font color='#D33022'>" + press + "</font>" + emailvar[i] + "</div>"
}
} else {
emailbefor = press.split("@")[0];
emailafter = "@" + press.split("@")[1];
for(var i=0; i<emailvar.length; i++) {
var theemail = emailvar[i];
if(theemail.indexOf(emailafter) == 0)
{
emailtxt = emailtxt + "<div class='newemail' style='width:170px; color:#6B6B6B; overflow:hidden;'><font color='#D33022'>" + emailbefor + "</font>" + emailvar[i] + "</div>"
}
}
}
$("#myemail").html(emailmy+emailtxt);
if($("#myemail").html()){
$("#myemail").css("display","block");
$(".newemail").css("width",$("#myemail").width());
can1press = true;
} else {
$("#myemail").css("display","none");
can1press = false;
}
beforepress = press;
}
if (press=="" || press==null){
$("#myemail").html("");
$("#myemail").css("display","none");
}
})
$(document).click(function(){ //文本框失焦時(shí)刪除層
if(can1press){
$("#myemail").remove();
can1press = false;
if($("#me").focus()){
can1press = false;
}
}
})
$(".newemail").live("mouseover",function(){ //鼠標(biāo)經(jīng)過提示Email時(shí),高亮該條Email
$(".newemail").css("background","#FFF");
$(this).css("background","#CACACA");
$(this).focus();
nowid = $(this).index();
}).live("click",function(){ //鼠標(biāo)點(diǎn)擊Email時(shí),文本框內(nèi)容替換成該條Email,并刪除提示層
var newhtml = $(this).html();
newhtml = newhtml.replace(/<.*?>/g,"");
$("#me").val(newhtml);
$("#myemail").remove();
})
$(document).bind("keydown",function(e)
{
if(can1press){
switch(e.which)
{
case 38:
if (nowid > 0){
$(".newemail").css("background","#FFF");
$(".newemail").eq(nowid).prev().css("background","#CACACA").focus();
nowid = nowid-1;
}
if(!nowid){
nowid = 0;
$(".newemail").css("background","#FFF");
$(".newemail").eq(nowid).css("background","#CACACA");
$(".newemail").eq(nowid).focus();
}
break;
case 40:
if (nowid < totalid){
$(".newemail").css("background","#FFF");
$(".newemail").eq(nowid).next().css("background","#CACACA").focus();
nowid = nowid+1;
}
if(!nowid){
nowid = 0;
$(".newemail").css("background","#FFF");
$(".newemail").eq(nowid).css("background","#CACACA");
$(".newemail").eq(nowid).focus();
}
break;
case 13:
var newhtml = $(".newemail").eq(nowid).html();
newhtml = newhtml.replace(/<.*?>/g,"");
$("#me").val(newhtml);
$("#myemail").remove();
}
}
})
})
//檢查email郵箱
function isEmail(str){
if(str.indexOf("@") > 0)
{
return true;
} else {
return false;
}
}
</script>
在輸入框中輸入“qq”、“Sina”、“163”等等可以看到效果
</body>
</html>
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery實(shí)現(xiàn)郵箱下拉列表自動(dòng)補(bǔ)全功能
- jQuery實(shí)現(xiàn)文本框郵箱輸入自動(dòng)補(bǔ)全效果
- jquery 實(shí)現(xiàn)輸入郵箱時(shí)自動(dòng)補(bǔ)全下拉提示功能
- 使用jquery實(shí)現(xiàn)仿百度自動(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)文章
實(shí)現(xiàn)placeholder效果的方案匯總
由于placeholder是html5的新屬性,可想而知,僅支持html5的瀏覽器才支持placeholder,目前最新的firefox、chrome、safari以及ie10都支持,ie6到ie9都不支持。2015-06-06
如何使用jQuery技術(shù)開發(fā)ios風(fēng)格的頁(yè)面導(dǎo)航菜單
這篇文章主要介紹了如何使用jQuery技術(shù)開發(fā)ios風(fēng)格的頁(yè)面導(dǎo)航菜單,需要的朋友可以參考下2015-07-07
淺談jquery上下滑動(dòng)的注意事項(xiàng)
下面小編就為大家?guī)硪黄猨query上下滑動(dòng)的注意事項(xiàng)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
如何從jQuery的ajax請(qǐng)求中刪除X-Requested-With
X-Requested-With常用于判斷是不是ajax請(qǐng)求,ajax請(qǐng)求中刪除X-Requested-With的方法如下,感興趣的朋友可以參考下2013-12-12
通過jQuery源碼學(xué)習(xí)javascript(一)
最近在做日志統(tǒng)計(jì)程序,發(fā)現(xiàn)對(duì)方的程序是在Jquery基礎(chǔ)上進(jìn)行開發(fā)的,而公司的網(wǎng)站的框架是prototype。而且我也早就想了解一下Jquery源碼,故決定研究Jquery源碼,模擬它的方法2012-12-12
jQuery+datatables插件實(shí)現(xiàn)ajax加載數(shù)據(jù)與增刪改查功能示例
這篇文章主要介紹了jQuery+datatables插件實(shí)現(xiàn)ajax加載數(shù)據(jù)與增刪改查功能,涉及jQuery結(jié)合datatables插件針對(duì)頁(yè)面表格實(shí)現(xiàn)數(shù)據(jù)加載及增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
動(dòng)態(tài)生成的DOM不會(huì)觸發(fā)onclick事件的原因及解決方法
下面小編就為大家?guī)硪黄獎(jiǎng)討B(tài)生成的DOM不會(huì)觸發(fā)onclick事件的原因及解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
jQuery.Validate 使用筆記(jQuery Validation范例 )
學(xué)習(xí)jQuery Validation,于是手寫一公共范例,并收藏以便后用,里面附有測(cè)試代碼,需要的朋友一起來測(cè)試。2010-06-06

