日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JS实现在输入框内输入@时,邮箱账号自动补全

發布時間:2023/11/29 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JS实现在输入框内输入@时,邮箱账号自动补全 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

<!DOCTYPE HTML>

<html lang="en">

<head>

<meta charset="utf-8"/>

<title>郵箱自動補全</title>

<style type="text/css">

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

h1{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>

<h1>郵箱自動補全 + 上下翻動</h1>

<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>


轉載于:https://blog.51cto.com/iicoo/1753944

總結

以上是生活随笔為你收集整理的JS实现在输入框内输入@时,邮箱账号自动补全的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。