Simple way to check if placeholder is supported?

后端 未结 9 894
执笔经年
执笔经年 2020-12-29 01:00

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 f

9条回答
  •  情书的邮戳
    2020-12-29 01:45

    NOTE: Placeholder DO NOT work in internet explorer in a way, it should work.

    document.createElement("input").placeholder == undefined
    

    Doesnt work in internet explorer 11 - document.createElement("input").placeholder return empty string


    var testInput = document.createElement('input');
    testPlaceholderCompatibility = ('placeholder' in testInput);
    

    Doesnt work in internet explorer 11 - return true


    'placeholder'in document.createElement("input")
    

    Doesnt work in internet explorer 11 - return true


    In theory, Internet explorer 11 is supposed to support placeholder, but in fact - when input get focus placeholder disappear. In Chrome placeholder showed until you actually type something, no matter on focus. So, feature detection doesnt work in this case - you need to detect IE and show Labels.

提交回复
热议问题