Simple way to check if placeholder is supported?

后端 未结 9 825
执笔经年
执笔经年 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.

    0 讨论(0)
  • 2020-12-29 01:52
    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.)

    0 讨论(0)
  • 2020-12-29 01:52

    Another way without making an input element in memory that has to be GC'd:

    if ('placeholder' in HTMLInputElement.prototype) {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题