欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery實現(xiàn)郵箱自動填充提示功能

 更新時間:2015年11月17日 11:58:39   作者:發(fā)表是最好的記憶  
這篇文章主要介紹了jquery實現(xiàn)郵箱自動填充提示功能,為了提高用戶的體驗,很多網(wǎng)站都會實現(xiàn)郵箱輸入的自動提示功能,對如何實現(xiàn)自動提示功能感興趣的小伙伴們可以參考一下

郵箱的廣泛使用得益于它的免費(fèi),因此很多網(wǎng)站在注冊的時候都會直接使用郵箱作為賬號名
為了提高用戶的體驗,很多網(wǎng)站都會實現(xiàn)郵箱輸入的自動提示功能,所有自己也實現(xiàn)了一個,先看下效果吧,覺得效果還行的就拿去

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

(function($){
 $.fn.mailAutoComplete = function(options){
 var defaults = {
  boxClass: "mailListBox", //外部box樣式
  listClass: "mailListDefault", //默認(rèn)的列表樣式
  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)建郵件內(nèi)部列表內(nèi)容
 $.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(); //獲取當(dāng)前對象(即文本框)的寬高
  //樣式的初始化
  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; //列表框?qū)ο?
  that.focus(function(){
  //父標(biāo)簽的層級
  $(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){
    //如果當(dāng)前有激活列表
    $(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){
    //如果當(dāng)前有激活列表
    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"); 
   
  }); 
  //鼠標(biāo)經(jīng)過列表項事件
  //鼠標(biāo)經(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);

頁面(這里就取一個div做實例):

<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>
<script type="text/javascript">
$("#email").mailAutoComplete({
boxClass: "out_box", //外部box樣式
listClass: "list_box", //默認(rèn)的列表樣式
focusClass: "focus_box", //列表選樣式中
markCalss: "mark_box", //高亮樣式
autoClass: false,
textHint: true //提示文字自動隱藏
});
</script>

還有點css,這個可能需要大家自己修改成自己想要的色調(diào)

<!-- 郵箱自動提示的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>

以上就是jquery實現(xiàn)郵箱自動填充提示功能的全部代碼,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • jquery 查找新建元素代碼

    jquery 查找新建元素代碼

    好久不用jquery,了,有的函數(shù)都忘記了,這里看下jquery下如何查找元素的。
    2010-07-07
  • jquery實現(xiàn)文本框的禁用和啟用

    jquery實現(xiàn)文本框的禁用和啟用

    本文主要介紹了jquery實現(xiàn)文本框的禁用和啟用,代碼清晰,有需要的朋友可以參考下,希望會對大家有所幫助
    2016-12-12
  • jquery實現(xiàn)員工管理注冊頁面

    jquery實現(xiàn)員工管理注冊頁面

    這篇文章主要為大家詳細(xì)介紹了jquery實現(xiàn)員工管理注冊頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • jQuery事件與動畫超詳細(xì)講解

    jQuery事件與動畫超詳細(xì)講解

    在jquery中,jquery動畫事件和動畫函數(shù)經(jīng)常用的到,今天小編抽時間給大家整理了些關(guān)于常用的jquery動畫事件和動畫函數(shù),對jquery動畫函數(shù)和動畫事件相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2022-12-12
  • JQuery Ajax執(zhí)行跨域請求數(shù)據(jù)的解決方案

    JQuery Ajax執(zhí)行跨域請求數(shù)據(jù)的解決方案

    今天小編就為大家分享一篇關(guān)于JQuery Ajax執(zhí)行跨域請求數(shù)據(jù)的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • jQuery插件擴(kuò)展extend的簡單實現(xiàn)原理

    jQuery插件擴(kuò)展extend的簡單實現(xiàn)原理

    下面小編就為大家?guī)硪黄猨Query插件擴(kuò)展extend的簡單實現(xiàn)原理。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • jQuery中:text選擇器用法實例

    jQuery中:text選擇器用法實例

    這篇文章主要介紹了jQuery中:text選擇器用法,以實例形式較為詳細(xì)的分析了text選擇器的功能、定義及匹配單行文本框的使用技巧,需要的朋友可以參考下
    2015-01-01
  • jquery獲取復(fù)選框checkbox的值實現(xiàn)方法

    jquery獲取復(fù)選框checkbox的值實現(xiàn)方法

    下面小編就為大家?guī)硪黄猨query獲取復(fù)選框checkbox的值實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • jQuery?表單事件與遍歷詳情

    jQuery?表單事件與遍歷詳情

    這篇文章主要介紹了jQuery?表單事件與遍歷詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • JQuery魔力之$("tagName")與selector

    JQuery魔力之$("tagName")與selector

    DOM 中的 getElementsByTagName()方法在JQuery中的表現(xiàn)就是$(“tagName”)這么簡單!tag標(biāo)簽(可以是:p、div、button …)標(biāo)簽本身具有ID、Class等屬性
    2012-03-03

最新評論