How to support placeholder attribute in IE8 and 9

I have a small issue, the placeholder attribute for input boxes is not supported in IE 8-9.

What is the best way to make this support in my project (ASP Net). I am using jQuery. Need I use some other external tools for it?

Is http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html a good solution?


You could use this jQuery plugin: https://github.com/mathiasbynens/jquery-placeholder

But your link seems to be also a good solution.


You can use any one of these polyfills:

  • https://github.com/jamesallardice/Placeholders.js (doesn't support password fields)
  • https://github.com/chemerisuk/better-placeholder-polyfill
  • These scripts will add support for the placeholder attribute in browsers that do not support it, and they do not require jQuery!


    the $.Browser.msie is not on the latest JQuery anymore... you have to use the $.support

    like below:

     <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>
    
    链接地址: http://www.djcxy.com/p/70548.html

    上一篇: ie7,ie8和ie9的最佳占位符polyfil脚本

    下一篇: 如何在IE8和9中支持占位符属性