登录  /  注册
首页 > web前端 > js教程 > 正文

jQuery实现Email邮箱地址自动补全功能代码

PHPz
发布: 2016-05-16 15:33:52
转载
2312人浏览过

这篇文章主要介绍了jquery实现email邮箱地址自动补全功能代码,涉及jquery鼠标事件及字符操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下,具体如下:

jQuery Email邮箱地址自动补全代码,输入Email时,会自动加入@符号,在输入框中输入“qq”、“Sina”、“163”等等可以看到效果;鼠标经过提示Email时,高亮该条Email,鼠标点击Email时,文本框内容替换成该条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("<p id=&#39;myemail&#39; style=&#39;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; &#39;></p>");
 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 = "<p class=&#39;newemail&#39; style=&#39;width:170px; color:#6B6B6B; overflow:hidden;&#39;><font color=&#39;#D33022&#39;>" + press + "</font></p>";
   if(!(isEmail(press))){
    for(var i=0; i<emailvar.length; i++) {
     emailtxt = emailtxt + "<p class=&#39;newemail&#39; style=&#39;width:170px; color:#6B6B6B; overflow:hidden;&#39;><font color=&#39;#D33022&#39;>" + press + "</font>" + emailvar[i] + "</p>"
    }
   } 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 + "<p class=&#39;newemail&#39; style=&#39;width:170px; color:#6B6B6B; overflow:hidden;&#39;><font color=&#39;#D33022&#39;>" + emailbefor + "</font>" + emailvar[i] + "</p>"
     }
    }
   }
   $("#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(){ //鼠标经过提示Email时,高亮该条Email
 $(".newemail").css("background","#FFF");
 $(this).css("background","#CACACA");
  $(this).focus();
  nowid = $(this).index();
 }).live("click",function(){ //鼠标点击Email时,文本框内容替换成该条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视频教程

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:脚本之家网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号