jQuery實現(xiàn)Email郵箱地址自動補(bǔ)全功能代碼
本文實例講述了jQuery實現(xiàn)Email郵箱地址自動補(bǔ)全功能代碼。分享給大家供大家參考,具體如下:
jQuery Email郵箱地址自動補(bǔ)全代碼,輸入Email時,會自動加入@符號,在輸入框中輸入“qq”、“Sina”、“163”等等可以看到效果;鼠標(biāo)經(jīng)過提示Email時,高亮該條Email,鼠標(biāo)點擊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相關(guān)字符自動提示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(){ //鼠標(biāo)經(jīng)過提示Email時,高亮該條Email $(".newemail").css("background","#FFF"); $(this).css("background","#CACACA"); $(this).focus(); nowid = $(this).index(); }).live("click",function(){ //鼠標(biāo)點擊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程序設(shè)計有所幫助。
- jQuery實現(xiàn)郵箱下拉列表自動補(bǔ)全功能
- jQuery實現(xiàn)文本框郵箱輸入自動補(bǔ)全效果
- jquery 實現(xiàn)輸入郵箱時自動補(bǔ)全下拉提示功能
- 使用jquery實現(xiàn)仿百度自動補(bǔ)全特效
- jquery實現(xiàn)郵箱自動補(bǔ)全功能示例分享
- PHP+jQuery實現(xiàn)自動補(bǔ)全功能源碼
- jQuery 插件autocomplete自動完成應(yīng)用(自動補(bǔ)全)(asp.net后臺)
- jquery實現(xiàn)翻動fadeIn顯示的方法
- jQuery實現(xiàn)Flash效果上下翻動的中英文導(dǎo)航菜單代碼
- jQuery仿Flash上下翻動的中英文導(dǎo)航菜單實例
- jQuery實現(xiàn)輸入框郵箱內(nèi)容自動補(bǔ)全與上下翻動顯示效果【附demo源碼下載】
相關(guān)文章
jQuery實現(xiàn)可展開合攏的手風(fēng)琴面板菜單
這篇文章主要介紹了jQuery實現(xiàn)可展開合攏的手風(fēng)琴面板菜單,涉及jQuery中slideUp及slideDown方法的相關(guān)使用技巧,代碼簡單實用,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09jquery實現(xiàn)Li滾動時滾動條自動添加樣式的方法
這篇文章主要介紹了jquery實現(xiàn)Li滾動時滾動條自動添加樣式的方法,實例分析了jquery響應(yīng)鼠標(biāo)事件及動態(tài)添加樣式的相關(guān)技巧,需要的朋友可以參考下2015-08-08jQuery提示插件qTip2用法分析(支持ajax及多種樣式)
這篇文章主要介紹了jQuery提示插件qTip2用法,結(jié)合實例形式分析了qTip2的使用技巧,可支持ajax及多種樣式的設(shè)置,需要的朋友可以參考下2016-06-06jquery實現(xiàn)的省市區(qū)三級聯(lián)動
在做項目的時候,我們經(jīng)常需要用到地址之類的省市區(qū)三級聯(lián)動的,今天就給大家分享一個非常簡潔的省市區(qū)三級聯(lián)動的代碼,基于jQuery,附上GIT地址,有需要的小伙伴可以直接拿走2015-04-04JQuery將文本轉(zhuǎn)化成JSON對象需要注意的問題
在JQuery的許多方法中,很多方法的參數(shù)可以傳入一個JSON對象,比如Ajax方法的第二個參數(shù)。怎么將文本轉(zhuǎn)化成JSON對象,需要注意以下問題2011-05-05