JS輸入用戶名自動顯示郵箱后綴列表的方法
本文實例講述了JS輸入用戶名自動顯示郵箱后綴列表的方法。分享給大家供大家參考。具體如下:
以下是代碼,保存到html文件打開:
<html>
<head>
<meta charset="UTF-8">
<title>輸入用戶名自動顯示郵箱后綴列表</title>
<script type="text/javascript" src="jquery/jquery-1.10.2.min.js"></script>
<style>
*{margin:0;padding:0;}
ul,li{list-style:none;}
.inputElem {width:198px;height:22px;line-height:22px;border:1px solid #ff4455;}
.parentCls{width:200px;}
.auto-tip li{width:100%;height:22px;line-height:22px;font-size:14px;}
.auto-tip li.hoverBg{background:#ddd;cursor:pointer;}
.red{color:red;}
.hidden {display:none;}
</style>
<script type="text/javascript" src="js/emailAutoComplete.js"></script>
</head>
<body>
<div style="width:736px;margin:10px auto;">
請在下方輸入郵箱用戶名:
<div class="parentCls">
<input type="text" class="inputElem">
</div>
</div>
</body>
</html>
原理是:一個輸入框 當(dāng)我輸入任何字的時候 自動下拉相應(yīng)的郵箱提示,在輸入框輸入11的時候 下拉框有所有11的郵箱 輸入其他的時候 有其他文案對應(yīng)的郵箱。
同理 此插件不需要任何html標(biāo)簽,只需要一個輸入框 有相對應(yīng)的class類名就ok 且父級有個class類名,其他的都不需要。內(nèi)部的HTML代碼都是自動生成的。
HTML代碼如下:
<input type="text" class="inputElem">
</div>
其實上面的div標(biāo)簽都可以不需要 只需要在input輸入框 且父級元素添加一個如上class(自定義也可以,只是在JS初始化的時候要傳入class就ok 我默認(rèn)情況下 父級class叫parentCls,當(dāng)前輸入框class叫inputElem,隱藏域的class叫hiddenCls,在初始化的時候 直接初始化 傳入空對象即可!)。因為頁面上可能有多個輸入框 所以需要一個父級class 來區(qū)分是那個輸入框,當(dāng)然要個隱藏域 存值給開發(fā)后臺。
其中在配置項里面 有個郵箱數(shù)組參數(shù) mailArr : ["@qq.com","@qq2.com","@gmail.com","@126.com","@163.com","@hotmail.com","@yahoo.com","@yahoo.com.cn","@live.com","@sohu.com","@sina.com"] 。就是要告訴我們默認(rèn)郵箱有這么多,不管我輸入什么 下拉框初始化時候有這么多郵箱提示,當(dāng)我精確到某一項的時候 在給個提示 精確到某一項下拉。當(dāng)然由于需求的變更 郵箱這個參數(shù)可以自己初始化時候 自己根據(jù)需求配置。
實現(xiàn)的功能如下:
1. 支持鍵盤上下移鍵盤操作,支持鼠標(biāo)點擊及按回車操作。
2. 點擊document時候 除當(dāng)前input輸入框之外 下拉框隱藏。當(dāng)接著輸入時候 實現(xiàn)自動匹配等等操作。
具體不多說 就是類似于網(wǎng)上注冊時候 郵箱自動提示功能一樣 ,如果有任何bug的話 可以給我留言,就不羅嗦了!
CSS代碼如下:
*{margin:0;padding:0;}
ul,li{list-style:none;}
.inputElem {width:198px;height:22px;line-height:22px;border:1px solid #ff4455;}
.parentCls{width:200px;}
.auto-tip li{width:100%;height:22px;line-height:22px;font-size:14px;}
.auto-tip li.hoverBg{background:#ddd;cursor:pointer;}
.red{color:red;}
.hidden {display:none;}
</style>
emailAutoComplete.js代碼點擊此處本站下載。
希望本文所述對大家的javascript程序設(shè)計有所幫助。
相關(guān)文章
JavaScript switch case 的用法實例[范圍]
JavaScript switch case 的用法實例,大家可以參考下。2009-09-09