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

IE下支持文本框和密碼框placeholder效果的JQuery插件分享

 更新時(shí)間:2015年01月31日 11:40:10   作者:沒(méi)有開花的樹  
這篇文章主要介紹了IE下支持文本框和密碼框placeholder效果的JQuery插件分享,本文給出插件源碼和使用示例,需要的朋友可以參考下

很久之前寫了這個(gè)插件,基于jQuery實(shí)現(xiàn)的,主要用于IE下實(shí)現(xiàn)placeholder效果,可同時(shí)支持文本和密碼輸入框。

placeholder是HTML5新增的一個(gè)屬性,當(dāng)input設(shè)置了該屬性后,該值的內(nèi)容將作為灰色提示顯示在文本框中,當(dāng)文本框獲得焦點(diǎn)時(shí),提示文字消失。

下載地址:http://xiazai.jb51.net/201501/other/placeholderfriend.rar

實(shí)現(xiàn)代碼如下:

復(fù)制代碼 代碼如下:

(function($) {
  /**
   * 沒(méi)有開花的樹
   * 2012/11/28 15:12
   */

  var placeholderfriend = {
    focus: function(s) {
      s = $(s).hide().prev().show().focus();
      var idValue = s.attr("id");
      if (idValue) {
        s.attr("id", idValue.replace("placeholderfriend", ""));
      }
      var clsValue = s.attr("class");
   if (clsValue) {
        s.attr("class", clsValue.replace("placeholderfriend", ""));
      }
    }
  }

  //判斷是否支持placeholder
  function isPlaceholer() {
    var input = document.createElement('input');
    return "placeholder" in input;
  }
  //不支持的代碼
  if (!isPlaceholer()) {
    $(function() {

      var form = $(this);

      //遍歷所有文本框,添加placeholder模擬事件
      var elements = form.find("input[type='text'][placeholder]");
      elements.each(function() {
        var s = $(this);
        var pValue = s.attr("placeholder");
  var sValue = s.val();
        if (pValue) {
          if (sValue == '') {
            s.val(pValue);
          }
        }
      });

      elements.focus(function() {
        var s = $(this);
        var pValue = s.attr("placeholder");
  var sValue = s.val();
        if (sValue && pValue) {
          if (sValue == pValue) {
            s.val('');
          }
        }
      });

      elements.blur(function() {
        var s = $(this);
        var pValue = s.attr("placeholder");
  var sValue = s.val();
        if (!sValue) {
          s.val(pValue);
        }
      });

      //遍歷所有密碼框,添加placeholder模擬事件
      var elementsPass = form.find("input[type='password'][placeholder]");
      elementsPass.each(function(i) {
        var s = $(this);
        var pValue = s.attr("placeholder");
  var sValue = s.val();
        if (pValue) {
          if (sValue == '') {
            //DOM不支持type的修改,需要復(fù)制密碼框?qū)傩?,生成新的DOM
            var html = this.outerHTML || "";
            html = html.replace(/\s*type=(['"])?password\1/gi, " type=text placeholderfriend")
              .replace(/\s*(?:value|on[a-z]+|name)(=(['"])?\S*\1)?/gi, " ")
              .replace(/\s*placeholderfriend/, " placeholderfriend value='" + pValue
              + "' " + "onfocus='placeholderfriendfocus(this);' ");
            var idValue = s.attr("id");
            if (idValue) {
              s.attr("id", idValue + "placeholderfriend");
            }
            var clsValue = s.attr("class");
   if (clsValue) {
              s.attr("class", clsValue + "placeholderfriend");
            }
            s.hide();
            s.after(html);
          }
        }
      });

      elementsPass.blur(function() {
        var s = $(this);
        var sValue = s.val();
        if (sValue == '') {
          var idValue = s.attr("id");
          if (idValue) {
            s.attr("id", idValue + "placeholderfriend");
          }
          var clsValue = s.attr("class");
    if (clsValue) {
            s.attr("class", clsValue + "placeholderfriend");
          }
          s.hide().next().show();
        }
      });

    });
  }
  window.placeholderfriendfocus = placeholderfriend.focus;
})(jQuery);

使用很簡(jiǎn)單,例子如下:

復(fù)制代碼 代碼如下:

<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script src="placeholderfriend.js" type="text/javascript"></script>
</head>
<body>
<input placeholder="賬號(hào)/手機(jī)號(hào)碼" ><br>
<input placeholder="密碼" type="password" >
</body>
</html>

相關(guān)文章

最新評(píng)論