且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用HTML5占位符属性时考虑向后兼容?

更新时间:2023-01-07 17:12:09

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

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.