jquery.mailAutoComplete 是一款輕量級 jQuery 外掛,在輸入框輸入「@」後自動彈出常見郵箱域名建議清單,支援鍵盤上下選擇、Enter 確認、滑鼠點擊、自訂樣式與提示文字。
<script src="jquery-1.6.min.js"></script> <!-- jQuery 核心 -->
<script src="jquery.mailAutoComplete-3.1.js"></script> <-- 外掛 -->
<input type="text" id="email" size="28" />
<script>
$(function() {
$("#email").mailAutoComplete(); // 預設支援 qq.com, gmail.com, 126.com 等 10 種
});
</script>
<style>
.out_box { border:1px solid #ccc; background:#fff; font:12px/20px Tahoma; }
.list_box { border-bottom:1px solid #eee; padding:0 5px; cursor:pointer; }
.focus_box { background:#f0f3f9; }
.mark_box { color:#c00; }
</style>
<input type="text" id="customTest" size="28" />
<script>
$(function() {
$("#customTest").mailAutoComplete({
boxClass: "out_box", // 外層容器 class
listClass: "list_box", // 列表項預設 class
focusClass: "focus_box", // 游標選中 class
markCalss: "mark_box", // 高亮標記 class(域名匹配部分)
autoClass: false, // false=不注入預設 CSS,改用自訂樣式
textHint: true, // 顯示提示文字
hintText: "請輸入郵箱地址", // 提示文字內容
mailArr: ["qq.com", "gmail.com", "163.com", "126.com", "company.com"] // 自訂域名
});
});
</script>
| 參數 | 預設值 | 說明 |
|---|---|---|
| boxClass | "mailListBox" | 下拉清單外層容器 class |
| listClass | "mailListDefault" | 清單項預設 class |
| focusClass | "mailListFocus" | 鍵盤/滑鼠選中項 class |
| markCalss | "mailListHlignt" | 域名匹配高亮部分 class |
| zIndex | 1 | 下拉層級 |
| autoClass | true | 是否自動注入預設 CSS |
| mailArr | 10 大常見域名 | 郵箱域名陣列 |
| textHint | false | 是否啟用提示文字 |
| hintText | "" | 提示文字內容 |
| focusColor | "#333" | 焦點時文字顏色 |
| blurColor | "#999" | 失焦時文字顏色 |
<span style="position:relative"> 中,建立絕對定位下拉容器。keyup 事件,解析「@」前後字串,過濾 mailArr 產生匹配清單。index,Enter 確認填入,滑鼠點擊直接填入。textHint 則恢復提示文字。transform 父層,或手動計算 offset() 定位。.mailAutoComplete()。