The mailbox drop down automatically populates the selected sample code figure
- 2020-03-30 02:30:59
- OfStack
1. You need a js file: jquery. Mailautocomplet-3.1.js
2. Of course, the jq library is indispensable, which is omitted here
So let's test that out
3. Style sheet:
4. Test the code
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201404/201404031547231.gif? 201433154738 ">
(function($){
$.fn.mailAutoComplete = function(options){
var defaults = {
boxClass: "mailListBox", //External box style
listClass: "mailListDefault", //The default list style
focusClass: "mailListFocus", //List of selected styles
markCalss: "mailListHlignt", //Highlighting the style
zIndex: 1,
autoClass: true, //Whether to use the built-in class style of the plug-in
mailArr: ["qq.com","gmail.com","126.com","163.com","hotmail.com","yahoo.com","yahoo.com.cn","live.com","sohu.com","sina.com"], //Mail array
textHint: false, //Automatic display and hiding of text prompt
hintText: "",
focusColor: "#333",
blurColor: "#999"
};
var settings = $.extend({}, defaults, options || {});
//The page loads CSS styles
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; //The plug-in's class variable
var z = settings.zIndex, newArr = mailArr = settings.mailArr, hint = settings.textHint, text = settings.hintText, fc = settings.focusColor, bc = settings.blurColor;
//Create the message internal list content
$.createHtml = function(str, arr, cur){
var mailHtml = "";
if($.isArray(arr)){
$.each(arr, function(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;
};
//Some global variables
var index = -1, s;
$(this).each(function(){
var that = $(this), i = $(".justForJs").size();
if(i > 0){ //Only one text box is bound
return;
}
var w = that.outerWidth(), h = that.outerHeight(); //Gets the width and height of the current object (that is, the text box)
//Style initialization
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; //List box object
that.focus(function(){
//Hierarchy of parent tags
$(this).css("color", fc).parent().css("z-index", z);
//Display and hide prompt 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 buttons are up and down
if(e.keyCode === 38){
// upward
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 the @ sign
//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 events
//The mouse after
$(".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. Of course, the jq library is indispensable, which is omitted here
So let's test that out
3. Style sheet:
<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>
4. Test the code
<p> The 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", //The default list style
focusClass: "focus_box", //List of selected styles
markCalss: "mark_box", //Highlighting the style
autoClass: false,
textHint: true, //Prompt text is automatically hidden
hintText: " Please enter your email address "
});
})
</script>
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201404/201404031547231.gif? 201433154738 ">