国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

jquery實現(xiàn)郵箱輸入自動提示功能:(一)

2024-04-27 15:01:39
字體:
來源:轉載
供稿:網(wǎng)友

記得去年做某個項目的時候,用到了郵箱輸入自動提示功能,于是網(wǎng)上搜了一下,發(fā)現(xiàn)了這個寫得不錯,現(xiàn)在回想起來,轉載一下,方便查閱。

郵箱的廣泛使用得益于它的免費,因此很多網(wǎng)站在注冊的時候都會直接使用郵箱作為賬號名

為了提高用戶的體驗,很多網(wǎng)站都會實現(xiàn)郵箱輸入的自動提示功能。

實現(xiàn)效果如圖所示:

核心代碼(需要jquery的支持):

(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"], //郵件數(shù)組            textHint: false, //文字提示的自動顯示與隱藏            hintText: "",            focusColor: "#333"            //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;        //創(chuàng)建郵件內部列表內容        $.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;        };        //一些全局變量        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('<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(function(){                //父標簽的層級                $(this).css("color", fc).parent().css("z-index", z);                    //提示文字的顯示與隱藏                if(hint && text){                    var focus_v = $.trim($(this).val());                    if(focus_v === text){                        $(this).val("");                    }                }                //鍵盤事件                $(this).keyup(function(e){                    s = v = $.trim($(this).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++;                        }else if(e.keyCode === 13){                            //回車                            if(index > -1 && index < newArr.length){                                //如果當前有激活列表                                $(this).val($("#mailList_"+index).text());                                }                        }else{                            if(/@/.test(v)){                                index = -1;                                //獲得@后面的值                                //s = v.replace(/@.*/, "");                                //創(chuàng)建新匹配數(shù)組                                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){                            //回車                            if(index > -1 && index < newArr.length){                                //如果當前有激活列表                                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");                                        });                    //鼠標經(jīng)過列表項事件                //鼠標經(jīng)過                $(".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);

html示例:

<div class="reg_lin1"><div class="lin1_1">常用郵箱:</div><div class="lin1_2"><input type="text" class = "reg_text" id = "email" name = "email"/></div><div class="lin1_3"></div></div>

調用的jquery代碼:

$("#email").mailAutoComplete({boxClass: "out_box", //外部box樣式listClass: "list_box", //默認的列表樣式focusClass: "focus_box", //列表選樣式中markCalss: "mark_box", //高亮樣式autoClass: false,textHint: true //提示文字自動隱藏});

css,這大家自己修改成自己想要的色調

  <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>

轉載地址:http://www.cnblogs.com/shenliang123/archive/2013/08/19/3267884.html


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 北票市| 黄大仙区| 江阴市| 剑川县| 金华市| 无极县| 兴安县| 扎赉特旗| 佛山市| 岳普湖县| 宜丰县| 嘉兴市| 桑日县| 新蔡县| 阳谷县| 金昌市| 北流市| 门头沟区| 西畴县| 彝良县| 琼海市| 革吉县| 江华| 霍林郭勒市| 广德县| 土默特左旗| 信宜市| 琼结县| 陆河县| 安西县| 关岭| 腾冲县| 宝坻区| 南郑县| 宾阳县| 舒兰市| 通海县| 临夏市| 云南省| 临夏市| 会同县|