1. Require a js file: jquery.mailAutoComplete-3.1.js
The code copy is as follows:
(function($){
$.fn.mailAutoComplete = function(options){
var defaults = {
boxClass: "mailListBox", //External box style
listClass: "mailListDefault", //Default list style
focusClass: "mailListFocus", //Select the style in the list
markCalss: "mailListHlignt", // Highlight style
zIndex: 1,
autoClass: true, //Whether to use the plug-in with its own class style
mailArr: ["qq.com","gmail.com","126.com","163.com","hotmail.com","yahoo.com","yahoo.com.cn","live. com","sohu.com","sina.com"], //Email array
textHint: false, // Automatic display and hide of text prompts
hintText: "",
focusColor: "#333",
blurColor: "#999"
};
var settings = $.extend({}, defaults, options || {});
//Page loading CSS style
if(settings.autoClass && $("#mailListAppendCss").size() === 0){
$('<style id="mailListAppendCss" type="text/css">.mailListBox{border:1px solid #369; background:#fff; font:12px/20px Arial;}.mail ListDefault{padding:0 5px;cursor :pointer;white-space:nowrap;}.mailListFocus{padding:0 5px;cursor:pointer;white-space:nowrap;background:#369;color:white;}. mailListHlignt{color:red;}.mailListFocus .mailListHlignt {color:#ffff;}</style>').appendTo($("head"));
}
var cb = settings.boxClass, cl = settings.listClass, cf = settings.focusClass, cm = settings.markCalss; //Plugin's class variable
var z = settings.zIndex, newArr = mailArr = settings.mailArr, hint = settings.textHint, text = settings.hintText, fc = settings.focusColor, bc = settin gs.blurColor;
//Create internal list content of the email
$.createHtml = function(str, arr, cur){
var mailHtml = "";
if($.isArray(arr)){
$.each(arr, function(i, n){
if(i === cur){
mailHtml += '<div id="mailList_'+i+'"><span>'+str+'</span>@'+arr[i]+'</div>';
}else{
mailHtml += '<div id="mailList_'+i+'"><span>'+str+'</span>@'+arr[i]+'</div>';
}
});
}
return mailHtml;
};
//Some global variables
var index = -1, s;
$(this).each(function(){
var that = $(this), i = $(".justForJs").size();
if(i > 0){ //Bind only one text box
return;
}
var w = that.outerWidth(), h = that.outerHeight(); //Get the width and height of the current object (i.e. text box)
//Style initialization
that.wrap('<span style="display:inline-block;position:relative;"></span>')
.before('<div id="mailListBox_'+i+'" style="min-width:'+w+'px;_width:'+w+'px;position:absolute;left:-6000px;top: '+h+ 'px;z-index:'+z+';"></div>');
var x = $("#mailListBox_" + i), liveValue; //Listbox object
that.focus(function(){
//The level of parent tag
$(this).css("color", fc).parent().css("z-index", z);
//Show and hide prompt text
if(hint && text){
var focus_v = $.trim($(this).val());
if(focus_v === text){
$(this).val("");
}
}
//Keyboard event
$(this).keyup(function(e){
s = v = $.trim($(this).val());
if(/@/.test(v)){
s = v.replace(/@.*/, "");
}
if(v.length > 0){
//If the key is up and down key
if(e.keyCode === 38){
//up
if(index <= 0){
index = newArr.length;
}
index--;
}else if(e.keyCode === 40){
//down
if(index >= newArr.length - 1){
index = -1;
}
index++;
}else if(e.keyCode === 13){
//Enter
if(index > -1 && index < newArr.length){
//If there is currently an activation list
$(this).val($("#mailList_"+index).text());
}
}else{
if(/@/.test(v)){
index = -1;
//Get the value after @
//s = v.replace(/@.*/, "");
//Create a new matching array
var site = v.replace(/.*@/, "");
newArr = $.map(mailArr, function(n){
var reg = new RegExp(site);
if(reg.test(n)){
return n;
}
});
}else{
newArr = mailArr;
}
}
x.html($.createHtml(s, newArr, index)).css("left", 0);
if(e.keyCode === 13){
//Enter
if(index > -1 && index < newArr.length){
//If there is currently an activation list
x.css("left", "-6000px");
}
}
}else{
x.css("left", "-6000px");
}
}).blur(function(){
if(hint && text){
var blur_v = $.trim($(this).val());
if(blur_v === ""){
$(this).val(text);
}
}
$(this).css("color", bc).unbind("keyup").parent().css("z-index",0);
x.css("left", "-6000px");
});
//The mouse passes the list item event
//The mouse passes
$(".mailHover").live("mouseover", function(){
index = Number($(this).attr("id").split("_")[1]);
liveValue = $("#mailList_"+index).text();
x.children("." + cf).removeClass(cf).addClass(cl);
$(this).addClass(cf).removeClass(cl);
});
});
x.bind("mousedown", function(){
that.val(liveValue);
});
});
};
})(jQuery);
2.jq library is of course essential, so here is omitted
Let's test it below
3. Style sheet:
The code copy is as follows:
<style type="text/css">
.out_box{border:1px solid #ccc; background:#ffff; 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>
4. Test code
The code copy is as follows:
<p>Custom class display: <input type="text" id="customTest" size="28" /></p>
<script src="js/jquery-1.6.min.js" type="text/javascript"></script>
<script src="js/jquery.mailAutoComplete-3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#customTest").mailAutoComplete({
boxClass: "out_box", //External box style
listClass: "list_box", //Default list style
focusClass: "focus_box", //Select the style in the list
markCalss: "mark_box", // Highlight style
autoClass: false,
textHint: true, //The prompt text is automatically hidden
hintText: "Please enter your email address"
});
})
</script>