如何使用HTML5占位符属性时考虑向后兼容? [英] how to use HTML5 placeholder attribute with backward-compatibility in mind?

查看:118
本文介绍了如何使用HTML5占位符属性时考虑向后兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用HTML5的占位符属性(您可以在思想结果看到它的阳离子的通讯) 。但是,当我用老的浏览器,当然,他们没有任何渲染。我可以使用JavaScript来模仿它,但后来,我不应该使用它,它做旧的方式。我怎样才能兼得HTML5占位符属性,并在同一时间进行模拟旧版本浏览器?

I'd like to use HTML5's placeholder attribute (You can see it in cation in the newsletter at Thought Results). But when I use older browsers, of course, they don't render anything. I can use JavaScript to imitate it, but then, I shouldn't use it and it's done the old way. How can I have both HTML5 placeholder attribute, and at the same time simulate it for older browsers?

推荐答案

您可以检测,如果浏览器支持的属性:

You can detect if a browser supports the attribute:

http://diveintohtml5.info/detect.html#input-placeholder

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

如果是的话,什么也不做。如果没有,你可以使用JS抢占位符值,然后将其插入到该领域,你认为合适(或许作为默认值),然后添加相应的相互作用来模拟HTML5占位符的行为。

If it does, do nothing. If it doesn't, you can use JS to grab the placeholder value and then insert it into the field as you see fit (perhaps as default value) and then add the appropriate interactions to simulate the HTML5 placeholder behaviors.

这篇关于如何使用HTML5占位符属性时考虑向后兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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