How to check, if a HTML5 input is supported?

后端 未结 4 1561
清歌不尽
清歌不尽 2021-01-11 12:47

I want to check in my website, if the visitors browser support a HTML5 input type. How should I do it?

相关标签:
4条回答
  • 2021-01-11 13:42

    i would comment on the accepted answer, but won't allow me without a certain reputation, so i'll do it this way. if a browser runs into an input type it doesn't recognize, it will automatically default to a 'text' input itself. so I would say the JS in the answer is unnecessary (unless you want to default to something other than 'text')

    0 讨论(0)
  • 2021-01-11 13:44

    Modernizr has support for checking the new input types.

    0 讨论(0)
  • 2021-01-11 13:49

    You could do:

    function IsAttributeSupported(tagName, attrName) {
        var val = false;
        // Create element
        var input = document.createElement(tagName);
        // Check if attribute (attrName)
        // attribute exists
        if (attrName in input) {
            val = true;
        }
        // Delete "input" variable to
        // clear up its resources
        delete input;
        // Return detected value
        return val;
    }
    
    if (!IsAttributeSupported("input", "placeholder")) {
        // Do something special here
        alert("placeholder attribute is not supported");
    }
    

    Hope it helps

    0 讨论(0)
  • 2021-01-11 13:50

    With the form fields you can use this:

    var i = document.createElement("input");
    i.setAttribute("type", "color");
    return i.type !== "text";
    

    If color is supported, i.type will be color but if it's not supported, the navigator returns text as default. So a simple verification like this can help you.

    0 讨论(0)
提交回复
热议问题