简单的方法来检查是否支持占位符? [英] Simple way to check if placeholder is supported?

查看:75
本文介绍了简单的方法来检查是否支持占位符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的代码中使用 HTML5 placeholder属性,如果用户的浏览器支持它,在窗体顶部打印字段名称。但我只想检查是否支持占位符,而不是用户使用的浏览器的版本/名称。

所以理想情况下,我想要做类似于

的操作>

 < body> 

< script>

if(placeholderIsNotSupported){
< b>用户名< / b>;
}
< / script>
< input type =textplaceholder =Username>
< / body>

除了我不确定javascript位。感谢您的帮助!

解决方案

  function placeholderIsSupported(){
var test =使用document.createElement( '输入');
return('placeholder'in test);
}

我使用了 jQuery版本作为起点。 (只要给出应有的抵用金额。)


I want to use the HTML5 "placeholder" attribute in my code if the user's browser supports it otherwise just print the field name on top of the form. But I only want to check whether placeholder is supported and not what version/name of browser the user is using.

So Ideally i would want to do something like

    <body>

     <script>

           if (placeholderIsNotSupported) {
             <b>Username</b>;
           } 
      </script>
    <input type = "text" placeholder ="Username">
</body>

Except Im not sure of the javascript bit. Help is appreciated!

解决方案

function placeholderIsSupported() {
    var test = document.createElement('input');
    return ('placeholder' in test);
}

I used a jQuery-ized version as a starting point. (Just giving credit where it's due.)

这篇关于简单的方法来检查是否支持占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆