【问题标题】:How to support placeholder attribute in IE8? [duplicate]如何在 IE8 中支持占位符属性? [复制]
【发布时间】:2014-05-16 04:52:22
【问题描述】:

我有一个小问题。 IE 8 不支持输入框的占位符属性。

有什么好方法可以增加对此的支持吗?

【问题讨论】:

    标签: php html css


    【解决方案1】:

    您可以随时使用其他插件,例如

    这些都行:)

    我建议第一个。它对我有用!

    最后 2 个不需要任何 JQuery !

    可能重复 -> How to support placeholder attribute in IE8 and 9

    【讨论】:

      【解决方案2】:

      好的,这里有一些非常快速、肮脏且未经测试的内容(字面意思是在我的手机上输入的),但这可能是您要走的路线。这个想法是在包含文本输入的元素上附加一个跨度,并通过 CSS 将其绝对“置于”文本输入的“上方”。您可以使用占位符和数据占位符将占位符文本放入输入中。您甚至可以只使用占位符。

      附: jQuery 不是强制性的。

      // include jQuery somewhere 
      
      function getPlaceholder(obj, parent=false) {
          var ph = obj.data('placeholder');
          if ( ph !== undefined && ph !== false) {
              if (!parent) {
                  parent = obj.parent();
              }
      
          if ( !parent.find(".ph").length ) {
              parent.append('<span class="ph">'+ph+'</span>');
          }
          }
      }
      
      // on page load
      $("input[type=text]").each(function() {
              getPlaceholder($(this));
      });
      
      $(document).on('change', 'input[type=text]', function() {
          var curVal = $(this).val();
          var phtext = $(this).parent().find(".ph");
      
          if (phtext.length) {
              if ( curVal === "" ) {
                  phtext.show();
              }
              else {
                  phtext.hide();
              }
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2013-02-07
        • 1970-01-01
        • 1970-01-01
        • 2011-07-29
        • 2012-06-05
        • 1970-01-01
        • 2011-10-26
        • 2011-04-25
        • 1970-01-01
        相关资源
        最近更新 更多