JS实现在输入框内输入@时,邮箱账号自动补全-创新互联

<!DOCTYPE HTML>

创新互联服务项目包括浦北网站建设、浦北网站制作、浦北网页制作以及浦北网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,浦北网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到浦北省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

<html lang="en">

<head>

<meta charset="utf-8"/>

<title>邮箱自动补全</title>

<style type="text/css">

.wrap{width:200px;margin:0 auto;}

h2{font-size:36px;text-align:center;line-height:60px;}

p{font-size:20px;text-align:center;line-height:60px;}

.inp{width:190px;border:1px solid #ccc;border-radius:5px;height:30px;line-height:30px;padding:5px;}

#AutoComplete{background:#fff;border:1px solid #4190db;display:none;width:200px;}

#AutoComplete ul{list-style-type:none;margin:0;padding:0;}

#AutoComplete li{color:#333;cursor:pointer;font:12px/22px \5b8b\4f53;text-indent:5px;}

#AutoComplete .hover{background:#6eb6fe;color:#fff;}

</style>

</head>

<body>

<h2>邮箱自动补全 + 上下翻动</h2>

<p>当在输入框内输入 @ 时,自动显示各个邮箱的下拉列表。</p>

<div class="wrap">

<form action="result.php" method="post">

<input type="text" name="email" id="email" class="inp" autocomplete="off"/><br/><br/>

<input type="text" name="other" class="inp" autocomplete="off"/><br/><br/>

<input type="submit" value="提交表单" id="submit"/>

</form>

</div>

<script type="text/javascript" src="jquery.min.js"></script>

<script type="text/javascript">

jQuery.AutoComplete = function(selector){

var elt = $(selector);

var autoComplete,autoLi;

var strHtml = [];

strHtml.push('<div class="AutoComplete" id="AutoComplete">');

strHtml.push(' <ul class="AutoComplete_ul">');

strHtml.push(' <li class="AutoComplete_title">请选择邮箱后缀</li>');

strHtml.push(' <li hz="@qq.com"></li>');

strHtml.push(' <li hz="@163.com"></li>');

strHtml.push(' <li hz="@126.com"></li>');

strHtml.push(' <li hz="@sohu.com"></li>');

strHtml.push(' <li hz="@sina.com"></li>');

strHtml.push(' </ul>');

strHtml.push('</div>');

$('body').append(strHtml.join(''));

autoComplete = $('#AutoComplete');

autoComplete.data('elt',elt);

autoLi = autoComplete.find('li:not(.AutoComplete_title)');

autoLi.mouseover(function(){

$(this).siblings().filter('.hover').removeClass('hover');

$(this).addClass('hover');

}).mouseout(function(){

$(this).removeClass('hover');

}).mousedown(function(){

autoComplete.data('elt').val($(this).text()).change();

autoComplete.hide();

});

//用户名补全+翻动

elt.keyup(function(e){

if(/13|38|40|116/.test(e.keyCode) || this.value == ''){

return false;

}

var username = this.value;

if(username.indexOf('@') == -1){

autoComplete.hide();

return false;

}

autoLi.each(function(){

this.innerHTML = username.replace(/\@+.*/,'') + $(this).attr('hz');

if(this.innerHTML.indexOf(username) >= 0){

$(this).show();

}else{

$(this).hide();

}

}).filter('.hover').removeClass('hover');

autoComplete.show().css({

left: $(this).offset().left,

top: $(this).offset().top + $(this).outerHeight(true) - 1,

position: 'absolute',

zIndex: '99999'

});

if(autoLi.filter(':visible').length == 0){

autoComplete.hide();

}else{

autoLi.filter(':visible').eq(0).addClass('hover');

}

}).keydown(function(e){

if(e.keyCode == 38){ //上

autoLi.filter('.hover').prev().not('.AutoComplete_title').addClass('hover').next().removeClass('hover');

}else if(e.keyCode == 40){ //下

autoLi.filter('.hover').next().addClass('hover').prev().removeClass('hover');

}else if(e.keyCode == 13){ //Enter

autoLi.filter('.hover').mousedown();

e.preventDefault(); //如有表单,阻止表单提交

}

}).focus(function(){

autoComplete.data('elt',$(this));

}).blur(function(){

autoComplete.hide();

});

}

$(function(){

$.AutoComplete('#email');

});

</script>

</body>

</html>

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网站栏目:JS实现在输入框内输入@时,邮箱账号自动补全-创新互联
本文地址:https://www.cdcxhl.com/article22/dcspjc.html

成都网站建设公司_创新互联,为您提供动态网站搜索引擎优化网站策划微信小程序响应式网站移动网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设