(function($){
$.fn.mailAutoComplete = (options){
var defaults = {
boxClass: "mailListBox",//外部box样式
listClass: "mailListDefault",1)">默认的列表样式
focusClass: "mailListFocus",1)">列表选样式中
markCalss: "mailListHlignt",1)">高亮样式
zIndex: 1,autoClass: true,1)">是否使用插件自带class样式
mailArr: ["qq.com","gmail.com","126.com","163.com","hotmail.com","yahoo.com","yahoo.com.cn","live.com","sohu.com","sina.com"],1)">邮件数组
textHint: false,1)">文字提示的自动显示与隐藏
hintText: ""blurColor: "#999"
};
var settings = $.extend({},defaults,options || {});
页面装载CSS样式
if(settings.autoClass && $("#mailListAppendCss").size() === 0){
$('<style id="mailListAppendCss" type="text/css">.mailListBox{border:1px solid #369; background:#fff; font:12px/20px Arial;}.mailListDefault{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:#fff;}</style>').appendTo($("head"));
}
var cb = settings.boxClass,cl = settings.listClass,cf = settings.focusClass,cm = settings.markCalss; 插件的class变量
var z = settings.zIndex,newArr = mailArr = settings.mailArr,hint = settings.textHint,text = settings.hintText,fc = settings.focusColor,bc = settings.blurColor;
创建邮件内部列表内容
$.createHtml = (str,arr,cur){
var mailHtml = "";
if($.isArray(arr)){
$.each(arr,(i,n){
if(i === cur){
mailHtml += '<div class="mailHover '+cf+'" id="mailList_'+i+'"><span class="'+cm+'">'+str+'</span>@'+arr[i]+'</div>';
}else{
mailHtml += '<div class="mailHover '+cl+'" id="mailList_'+i+'"><span class="'+cm+'">'+str+'</span>@'+arr[i]+'</div>';
}
});
}
return mailHtml;
};
一些全局变量
var index = -1this).each((){
var that = $(this),i = $(".justForJs").size();
if(i > 0){ 只绑定一个文本框
;
}
var w = that.outerWidth(),h = that.outerHeight(); 获取当前对象(即文本框)的宽高
样式的初始化
that.wrap('<span style="display:inline-block;position:relative;"></span>')
.before('<div id="mailListBox_'+i+'" class="justForJs '+cb+'" style="min-width:'+w+'px;_width:'+w+'px;position:absolute;left:-6000px;top:'+h+'px;z-index:'+z+';"></div>');
var x = $("#mailListBox_" + i),liveValue; 列表框对象
that.focus((){
父标签的层级
$(this).css("color",fc).parent().css("z-index"提示文字的显示与隐藏
if(hint && text){
var focus_v = $.trim($(this).val());
if(focus_v === text){
$(this).val("");
}
}
键盘事件
$(this).keyup((e){
s = v = $.trim($().val());
if(/@/.test(v)){
s = v.replace(/@.*/,"");
}
if(v.length > 0){
如果按键是上下键
if(e.keyCode === 38){
向上
if(index <= 0){
index = newArr.length;
}
index--;
}else if(e.keyCode === 40向下
if(index >= newArr.length - 1){
index = -1;
}
index++if(e.keyCode === 13回车
if(index > -1 && index < newArr.length){
如果当前有激活列表
$(this).val($("#mailList_"+index).text());
}
}{
.test(v)){
index = -1;
获得@后面的值
s = v.replace(/@.*/,"");
创建新匹配数组
var site = v.replace(/.*@/,1)">);
newArr = $.map(mailArr,1)">(n){
var reg = new RegExp(site);
(reg.test(n)){
n;
}
});
}{
newArr = mailArr;
}
}
x.html($.createHtml(s,newArr,index)).css("left",0);
如果当前有激活列表
x.css("left","-6000px");
}
}
}{
x.css("left",1)">);
}
}).blur((){
text){
var blur_v = $.trim($().val());
if(blur_v === ""){
$().val(text);
}
}
$();
x.css("left",1)">);
});
鼠标经过列表项事件
鼠标经过
$(".mailHover").live("mouSEOver",1)">(){
index = Number($(this).attr("id").split("_")[1]);
liveValue = $("#mailList_"+index).text();
x.children("." + cf).removeClass(cf).addClass(cl);
$().addClass(cf).removeClass(cl);
});
});
x.bind("mousedown",1)">(){
that.val(liveValue);
});
});
};
})(jQuery);
<div class="reg_lin1">
="lin1_1">常用邮箱:</div="lin1_2"><input type="text" class = "reg_text" id = "email" name = "email"/></="lin1_3"></>
$("#email").mailAutoComplete({
boxClass: "out_box",1)">外部box样式
listClass: "list_box",1)">默认的列表样式
focusClass: "focus_box",1)">列表选样式中
markCalss: "mark_box",1)">高亮样式
autoClass: falsetrue 提示文字自动隐藏
});
<style type="text/css">
.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>