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

在線演示地址如下:
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相關字符自動提示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(){ //文本框獲得焦點,插入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(){ //文本框輸入文字時,顯示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(){ //文本框失焦時刪除層
if(can1press){
$("#myemail").remove();
can1press = false;
if($("#me").focus()){
can1press = false;
}
}
})
$(".newemail").live("mouseover",function(){ //鼠標經(jīng)過提示Email時,高亮該條Email
$(".newemail").css("background","#FFF");
$(this).css("background","#CACACA");
$(this).focus();
nowid = $(this).index();
}).live("click",function(){ //鼠標點擊Email時,文本框內(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>
希望本文所述對大家jQuery程序設計有所幫助。
- jQuery實現(xiàn)郵箱下拉列表自動補全功能
- jQuery實現(xiàn)文本框郵箱輸入自動補全效果
- jquery 實現(xiàn)輸入郵箱時自動補全下拉提示功能
- 使用jquery實現(xiàn)仿百度自動補全特效
- jquery實現(xiàn)郵箱自動補全功能示例分享
- PHP+jQuery實現(xiàn)自動補全功能源碼
- jQuery 插件autocomplete自動完成應用(自動補全)(asp.net后臺)
- jquery實現(xiàn)翻動fadeIn顯示的方法
- jQuery實現(xiàn)Flash效果上下翻動的中英文導航菜單代碼
- jQuery仿Flash上下翻動的中英文導航菜單實例
- jQuery實現(xiàn)輸入框郵箱內(nèi)容自動補全與上下翻動顯示效果【附demo源碼下載】
相關文章
如何使用jQuery技術開發(fā)ios風格的頁面導航菜單
這篇文章主要介紹了如何使用jQuery技術開發(fā)ios風格的頁面導航菜單,需要的朋友可以參考下2015-07-07
如何從jQuery的ajax請求中刪除X-Requested-With
X-Requested-With常用于判斷是不是ajax請求,ajax請求中刪除X-Requested-With的方法如下,感興趣的朋友可以參考下2013-12-12
jQuery+datatables插件實現(xiàn)ajax加載數(shù)據(jù)與增刪改查功能示例
這篇文章主要介紹了jQuery+datatables插件實現(xiàn)ajax加載數(shù)據(jù)與增刪改查功能,涉及jQuery結合datatables插件針對頁面表格實現(xiàn)數(shù)據(jù)加載及增刪改查等相關操作技巧,需要的朋友可以參考下2018-04-04
動態(tài)生成的DOM不會觸發(fā)onclick事件的原因及解決方法
下面小編就為大家?guī)硪黄獎討B(tài)生成的DOM不會觸發(fā)onclick事件的原因及解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
jQuery.Validate 使用筆記(jQuery Validation范例 )
學習jQuery Validation,于是手寫一公共范例,并收藏以便后用,里面附有測試代碼,需要的朋友一起來測試。2010-06-06

