Internet Explorer 的输入占位符

IT技术 javascript html internet-explorer placeholder
2021-01-28 21:52:15

HTML5placeholderinput元素引入了属性,它允许显示灰色的默认文本。

遗憾的是,包括 IE 9 在内的 Internet Explorer 不支持它。

已经有一些占位符模拟器脚本了。它们通常通过将默认文本放入输入字段来工作,将其设置为灰色,并在您聚焦输入字段后立即将其删除。

这种方法的缺点是占位符文本位于输入字段中。因此:

  1. 脚本无法轻松检查输入字段是否为空
  2. 服务器端处理必须检查默认值,以便不将占位符插入到数据库中。

我想要一个解决方案,其中占位符文本不在输入本身中。

6个回答

在查看HTML5 Cross Browser Polyfills的“Web 表单:输入占位符”部分时,我看到的是jQuery-html5-placeholder

用 IE9尝试了这个演示,它看起来像是<input>用一个跨度包裹你,并用占位符文本覆盖一个标签。

<label>Text:
  <span style="position: relative;">
    <input id="placeholder1314588474481" name="text" maxLength="6" type="text" placeholder="Hi Mom">
    <label style="font: 0.75em/normal sans-serif; left: 5px; top: 3px; width: 147px; height: 15px; color: rgb(186, 186, 186); position: absolute; overflow-x: hidden; font-size-adjust: none; font-stretch: normal;" for="placeholder1314588474481">Hi Mom</label>
  </span>
</label>

那里还有其他垫片,但我没有全部看。其中之一,Placeholders.js,将自己宣传为“没有依赖项(因此不需要包含 jQuery,与大多数占位符 polyfill 脚本不同)。”

编辑:对于那些对“如何”和“什么”更感兴趣的人,如何创建高级 HTML5 占位符 polyfill,它会完成创建执行此操作的 jQuery 插件的过程。

此外,请参阅在 IE10 中保持占位符处于焦点上,以获取有关占位符文本如何在 IE10中消失的评论,这与 Firefox 和 Chrome 不同。不确定是否有解决此问题的方法。

该插件不再维护,并且有很多已知问题。
2021-03-12 21:52:15
不是太大的风扇导致输入被修改,我建议查看下面尼克的答案。它使用透明覆盖而不是修改输入的值
2021-03-26 21:52:15
否决?答案包含指向其他垫片的链接。更新为包含一个非 jQuery 的。
2021-03-31 21:52:15
插件的github页面不再存在。这是另一个
2021-04-03 21:52:15
在 IE7、IE8 和 IE9 中对我来说效果很好。
2021-04-07 21:52:15

我的经验中最好的一个是https://github.com/mathiasbynens/jquery-placeholder(由html5please.com推荐)。http://afarkas.github.com/webshim/demos/index.html在其更广泛的 polyfills 库中也有一个很好的解决方案。

在 jQuery-html5-placeholder 上 +1。这看起来好多了,并处理了另一个错过的一些边缘情况(例如隐藏字段)
2021-03-16 21:52:15
不得不说这次 StackOverflow 提供了一个糟糕的建议。当您在 IE 中单击时,jQuery 占位符不会清除输入。
2021-03-24 21:52:15

使用 jQuery 实现,您可以在提交时轻松删除默认值。下面是一个例子:

$('#submit').click(function(){
   var text = this.attr('placeholder');
   var inputvalue = this.val();  // you need to collect this anyways
   if (text === inputvalue) inputvalue = "";

   // $.ajax(...  // do your ajax thing here

});

我知道您正在寻找叠加层,但您可能更喜欢这条路线的简便性(现在知道我上面写的内容)。如果是这样,那么我为我自己的项目编写了它,它的效果非常好(需要 jQuery),并且只需几分钟即可为您的整个站点实现。它首先提供灰色文本,对焦时提供浅灰色,打字时提供黑色。每当输入字段为空时,它还提供占位符文本。

首先设置您的表单并在输入标签上包含您的占位符属性。

<input placeholder="enter your email here">

只需复制此代码并将其保存为 placeholder.js。

(function( $ ){

   $.fn.placeHolder = function() {  
      var input = this;
      var text = input.attr('placeholder');  // make sure you have your placeholder attributes completed for each input field
      if (text) input.val(text).css({ color:'grey' });
      input.focus(function(){  
         if (input.val() === text) input.css({ color:'lightGrey' }).selectRange(0,0).one('keydown', function(){     
            input.val("").css({ color:'black' });  
         });
      });
      input.blur(function(){ 
         if (input.val() == "" || input.val() === text) input.val(text).css({ color:'grey' }); 
      }); 
      input.keyup(function(){ 
        if (input.val() == "") input.val(text).css({ color:'lightGrey' }).selectRange(0,0).one('keydown', function(){
            input.val("").css({ color:'black' });
        });               
      });
      input.mouseup(function(){
        if (input.val() === text) input.selectRange(0,0); 
      });   
   };

   $.fn.selectRange = function(start, end) {
      return this.each(function() {
        if (this.setSelectionRange) { this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true); 
            range.moveEnd('character', end); 
            range.moveStart('character', start); 
            range.select(); 
        }
      });
   };

})( jQuery );

仅在一个输入上使用

$('#myinput').placeHolder();  // just one

当浏览器不支持 HTML5 占位符属性时,我建议您在站点上的所有输入字段上实现它:

var placeholder = 'placeholder' in document.createElement('input');  
if (!placeholder) {      
  $.getScript("../js/placeholder.js", function() {   
      $(":input").each(function(){   // this will work for all input fields
        $(this).placeHolder();
      });
  });
} 
它是否支持密码字段。
2021-03-28 21:52:15
您能否为上述内容创建一个小提琴/演示,也将帮助我和未来的观众。
2021-04-03 21:52:15

在尝试了一些建议并在 IE 中看到问题之后,这里是一个有效的方法:

https://github.com/parndt/jquery-html5-placeholder-shim/

我喜欢什么 - 你只需要包含 js 文件。无需启动它或任何东西。

使用此占位符后,在 ie 中工作正常,但如果表单中有任何错误,则在显示表单错误时,占位符文本与输入字段未对齐,我尝试解决但无法解决。您能指导我如何解决吗.
2021-03-19 21:52:15
我真的比值修改版本更喜欢这个。我希望占位符的 html5 版本也能这样工作!(即离开占位符直到输入一些文本,而不是失去焦点上的值)
2021-03-20 21:52:15
  • 仅适用于 IE9+

以下解决方案绑定到具有占位符属性的输入文本元素。它模拟仅适用于 IE 的占位符行为,如果未更改,则在提交时清除输入的值字段。

添加此脚本,IE 似乎支持 HTML5 占位符。

  $(function() {
  //Run this script only for IE
  if (navigator.appName === "Microsoft Internet Explorer") {
    $("input[type=text]").each(function() {
      var p;
     // Run this script only for input field with placeholder attribute
      if (p = $(this).attr('placeholder')) {
      // Input field's value attribute gets the placeholder value.
        $(this).val(p);
        $(this).css('color', 'gray');
        // On selecting the field, if value is the same as placeholder, it should become blank
        $(this).focus(function() {
          if (p === $(this).val()) {
            return $(this).val('');
          }
        });
         // On exiting field, if value is blank, it should be assigned the value of placeholder
        $(this).blur(function() {
          if ($(this).val() === '') {
            return $(this).val(p);
          }
        });
      }
    });
    $("input[type=password]").each(function() {
      var e_id, p;
      if (p = $(this).attr('placeholder')) {
        e_id = $(this).attr('id');
        // change input type so that the text is displayed
        document.getElementById(e_id).type = 'text';
        $(this).val(p);
        $(this).focus(function() {
          // change input type so that password is not displayed
          document.getElementById(e_id).type = 'password';
          if (p === $(this).val()) {
            return $(this).val('');
          }
        });
        $(this).blur(function() {
          if ($(this).val() === '') {
            document.getElementById(e_id).type = 'text';
            $(this).val(p);
          }
        });
      }
    });
    $('form').submit(function() {
      //Interrupt submission to blank out input fields with placeholder values
      $("input[type=text]").each(function() {
        if ($(this).val() === $(this).attr('placeholder')) {
          $(this).val('');
        }
      });
     $("input[type=password]").each(function() {
        if ($(this).val() === $(this).attr('placeholder')) {
           $(this).val('');
        }
      });
    });
  }
});
许多用户仍然使用IE8,有些甚至使用IE7。所以如果它不支持他们,我就不会去做
2021-03-25 21:52:15