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

首頁 > 編程 > JavaScript > 正文

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

2019-11-20 13:16:04
字體:
來源:轉載
供稿:網友

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

placeholder是HTML5新增的一個屬性,當input設置了該屬性后,該值的內容將作為灰色提示顯示在文本框中,當文本框獲得焦點時,提示文字消失。

下載地址:http://xiazai.VeVB.COm/201501/other/placeholderfriend.rar

實現代碼如下:

復制代碼 代碼如下:

(function($) {
  /**
   * 沒有開花的樹
   * 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的修改,需要復制密碼框屬性,生成新的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);

使用很簡單,例子如下:

復制代碼 代碼如下:

<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script src="placeholderfriend.js" type="text/javascript"></script>
</head>
<body>
<input placeholder="賬號/手機號碼" ><br>
<input placeholder="密碼" type="password" >
</body>
</html>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 井陉县| 时尚| 稷山县| 洛阳市| 安国市| 滦南县| 昌都县| 景德镇市| 木兰县| 郧西县| 宕昌县| 资兴市| 杭州市| 上杭县| 滨州市| 利辛县| 宜良县| 合阳县| 榆中县| 卢龙县| 理塘县| 丰顺县| 泉州市| 察雅县| 柳河县| 金沙县| 修武县| 子洲县| 县级市| 甘肃省| 五家渠市| 宜黄县| 巴楚县| 兰坪| 余姚市| 馆陶县| 菏泽市| 南江县| 陇川县| 潮州市| 柯坪县|