如何在IE8和9中支持占位符属性

Arb*_*æde 131 html5 internet-explorer placeholder

我有一个小问题,placeholderIE 8-9不支持输入框的属性.

在我的项目(ASP Net)中提供此支持的最佳方法是什么.我正在使用jQuery.我需要使用其他一些外部工具吗?

http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html好的解决办法?

sta*_*abs 89

您可以使用以下任何一种填充物:

这些脚本将placeholder在不支持它的浏览器中添加对属性的支持,并且它们不需要jQuery!

  • 我支持这个因为我喜欢非jquery解决方案的想法,但是现在这个代码在IE8中存在问题所以它不适合我.https://github.com/jamesallardice/Placeholders.js/issues/17 (4认同)
  • 此解决方案不处理密码框 (3认同)
  • 这是IE8中唯一适合我的解决方案 (2认同)
  • @Jurik我添加了一个额外的polyfill - 我希望它有所帮助. (2认同)

red*_*ert 88

你可以使用这个jQuery插件:https: //github.com/mathiasbynens/jquery-placeholder

但你的链接似乎也是一个很好的解决方案.

  • 在我的IE8密码占位符是点 (6认同)
  • 问题中提出的链接对我不起作用; 它在表单提交方面存在问题.你在这个答案中建议的解决方案看起来更加强大并且有效. (2认同)

小智 24

$ .Browser.msie是不是最新的JQuery了...你必须使用$.支持

如下:

 <script>

     (function ($) {
         $.support.placeholder = ('placeholder' in document.createElement('input'));
     })(jQuery);


     //fix for IE7 and IE8
     $(function () {
         if (!$.support.placeholder) {
             $("[placeholder]").focus(function () {
                 if ($(this).val() == $(this).attr("placeholder")) $(this).val("");
             }).blur(function () {
                 if ($(this).val() == "") $(this).val($(this).attr("placeholder"));
             }).blur();

             $("[placeholder]").parents("form").submit(function () {
                 $(this).find('[placeholder]').each(function() {
                     if ($(this).val() == $(this).attr("placeholder")) {
                         $(this).val("");
                     }
                 });
             });
         }
     });
 </script>
Run Code Online (Sandbox Code Playgroud)

  • 这就像一个魅力,我喜欢它比添加插件更好,非常感谢 (2认同)