DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JS輸入用戶名自動顯示郵箱後綴列表
JS輸入用戶名自動顯示郵箱後綴列表
編輯:關於JavaScript     

點擊這裡體驗效果

以下是代碼,保存到html文件打開:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>輸入用戶名自動顯示郵箱後綴列表-</title>
<script type="text/javascript" src="http:///keleyi/pmedia/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="http:///keleyi/phtml/jstexiao/7/emailAutoComplete.js"></script>
</head>
<body>
<div style="width:736px;margin:10px auto;">
<div><h2>輸入用戶名自動顯示郵箱後綴列表·</h2><a href="http:///a/bjac/xvcdrv0w.htm" target="_blank">原文</a></div>
請在下方輸入郵箱用戶名:
<div class="parentCls">
<input type="text" class="inputElem">
</div>
</div>
</body>
</html>



原理是:一個輸入框 當我輸入任何字的時候 自動下拉相應的郵箱提示,在輸入框輸入11的時候 下拉框有所有11的郵箱 輸入其他的時候 有其他文案對應的郵箱。

同理 此插件不需要任何html標簽,只需要一個輸入框 有相對應的class類名就ok 且父級有個class類名,其他的都不需要。內部的HTML代碼都是自動生成的。

HTML代碼如下:

<div class="parentCls">
<input type="text" class="inputElem">
</div>

其實上面的div標簽都可以不需要 只需要在input輸入框 且父級元素添加一個如上class(自定義也可以,只是在JS初始化的時候要傳入class就ok 我默認情況下 父級class叫parentCls,當前輸入框class叫inputElem,隱藏域的class叫hiddenCls,在初始化的時候 直接初始化 傳入空對象即可!)。因為頁面上可能有多個輸入框 所以需要一個父級class 來區分是那個輸入框,當然要個隱藏域 存值給開發後台。

其中在配置項裡面 有個郵箱數組參數 mailArr : ["@qq.com","@qq2.com","@gmail.com","@126.com","@163.com","@hotmail.com","@yahoo.com","@yahoo.com.cn","@live.com","@sohu.com","@sina.com"] 。就是要告訴我們默認郵箱有這麼多,不管我輸入什麼 下拉框初始化時候有這麼多郵箱提示,當我精確到某一項的時候 在給個提示 精確到某一項下拉。當然由於需求的變更 郵箱這個參數可以自己初始化時候 自己根據需求配置。


實現的功能如下:

1. 支持鍵盤上下移鍵盤操作,支持鼠標點擊及按回車操作。

2. 點擊document時候 除當前input輸入框之外 下拉框隱藏。當接著輸入時候 實現自動匹配等等操作。

具體不多說 就是類似於網上注冊時候 郵箱自動提示功能一樣 ,如果有任何bug的話 可以給我留言,時間也不早了 不羅嗦了!

CSS代碼如下:
<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>

emailAutoComplete.js代碼
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved