0

我才知道jquery支持所有包括旧浏览器

但我试图为搜索框添加一个占位符,但它在 IE8 中不起作用

jQuery(document).ready(function(){
    $('#search').attr('placeholder','search....');                          
});

所以我想了解更多关于跨浏览器支持的信息。如前所述,它实际上是否有效?


placeholder我知道不支持html 属性。但我的问题是关于 jquery 的。

4

4 回答 4

2

占位符是 IE8 不支持的 HTML5 功能

使用以下

<input type="text" value="search..." onfocus="if(this.value == 'search...') this.value = '';" onblur="if(this.value == '') this.value = 'search...';" />

使用 jQuery

<input type="text" class="my-input" value=""  />

    <script type="text/javascript">

        $(document).ready(function () {
            $('.my-input').val('search...');
            $('.my-input').focus(function () {
                if (this.value == 'search...') this.value = '';
            });
            $('input.my-input').blur(function () {
                if (this.value == '') this.value = 'search...'
            });
        });
        /*
        here I assumed a class
        .my-input 

        you can use input#id or input.className or other selector

        better you give the code of your text-box or entire form so that I can give you the exact selector

        */
    </script>
于 2013-11-01T08:34:50.303 回答
1

不支持如上占位符。
因此,将以下代码/注释添加到 HTML 页面的头部。现在 ie8 会玩得很好。

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<![endif]-->
于 2013-11-01T08:24:14.327 回答
0

这里的问题不是 JQuery,而是占位符属性本身。有关您的问题的答案,请参阅此图表:,IE8 不支持占位符属性

于 2013-11-01T08:24:35.000 回答
0

IE8 不支持占位符

只有 IE10+ 支持这个属性

但是,如果您真的想要占位符,请查看这个 jQuery 插件https://github.com/mathiasbynens/jquery-placeholder

于 2013-11-01T08:20:09.953 回答