Home > Web Front-end > JS Tutorial > Email drop-down autofill selection sample code with picture_javascript skills

Email drop-down autofill selection sample code with picture_javascript skills

WBOY
Release: 2016-05-16 16:53:36
Original
1178 people have browsed it

1. A js file is required: jquery.mailAutoComplete-3.1.js

Copy code The code is as follows:

(function($){
$.fn.mailAutoComplete = function(options){
var defaults = {
boxClass: "mailListBox", //外部box样式
listClass: "mailListDefault", //默认的列表样式
focusClass: "mailListFocus", //列表选样式中
markCalss: "mailListHlignt", //高亮样式
zIndex: 1,
autoClass: true, //是否使用插件自带class样式
mailArr: ["qq.com","gmail.com","126.com","163.com","hotmail.com","yahoo.com","yahoo.com.cn","live.com","sohu.com","sina.com"], //邮件数组
textHint: false, //文字提示的自动显示与隐藏
hintText: "",
focusColor: "#333",
blurColor: "#999"
};
var settings = $.extend({}, defaults, options || {});

//页面装载CSS样式
if(settings.autoClass && $("#mailListAppendCss").size() === 0){
$('').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 = function(str, arr, cur){
var mailHtml = "";
if($.isArray(arr)){
$.each(arr, function(i, n){
if(i === cur){
mailHtml += '
'+str+'@'+arr[i]+'
';
}else{
mailHtml += '
'+str+'@'+arr[i]+'
';
}
});
}
return mailHtml;
};
//一些全局变量
var index = -1, s;
$(this).each(function(){
var that = $(this), i = $(".justForJs").size();
if(i > 0){ //只绑定一个文本框
return;
}
var w = that.outerWidth(), h = that.outerHeight(); //获取当前对象(即文本框)的宽高
//样式的初始化
that.wrap('')
.before('
');
var x = $("#mailListBox_" i), liveValue; //List box object
that.focus(function(){
//Level of parent tag
$ (this).css("color", fc).parent().css("z-index", z);
//Display and hide hint text
if(hint && text){
var focus_v = $.trim($(this).val());
if(focus_v === text){
$(this).val("");
}
}
//Keyboard events
$(this).keyup(function(e){
s = v = $.trim($(this).val());
if (/@/.test(v)){
s = v.replace(/@.*/, "");
}
if(v.length > 0){
//If the key is an up or 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 active 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");

});
//Mouse over list item event
//Mouse over
$(".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, here it is Omit

Let’s test it below

3. Style sheet:
Copy code The code is as follows :



4. Test code
Copy code The code is as follows:

Custom class display:







Email drop-down autofill selection sample code with picture_javascript skills
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template