Check if html element is supported

被刻印的时光 ゝ 提交于 2021-02-07 02:48:20

问题


How to check if html element is supported, datalist for example? MDC says it is supoorted only in Opera and FireFox.


回答1:


if ('options' in document.createElement('datalist')) {
    // supported!
}

http://diveintohtml5.info/everything.html#datalist




回答2:


If someone needs to check for support of other HTML5 elements this could also be used.

https://github.com/ryanmorr/is-element-supported

From http://ryanmorr.com/determine-html5-element-support-in-javascript/

/*
 * isElementSupported
 * Feature test HTML element support 
 * @param {String} tag
 * @return {Boolean|Undefined}
 */

(function(win){
    'use strict';       

    var toString = {}.toString;

    win.isElementSupported = function isElementSupported(tag) {
        // Return undefined if `HTMLUnknownElement` interface
        // doesn't exist
        if (!win.HTMLUnknownElement) {
            return undefined;
        }
        // Create a test element for the tag
        var element = document.createElement(tag);
        // Check for support of custom elements registered via
        // `document.registerElement`
        if (tag.indexOf('-') > -1) {
            // Registered elements have their own constructor, while unregistered
            // ones use the `HTMLElement` or `HTMLUnknownElement` (if invalid name)
            // constructor (http://stackoverflow.com/a/28210364/1070244)
            return (
                element.constructor !== window.HTMLUnknownElement &&
                element.constructor !== window.HTMLElement
            );
        }
        // Obtain the element's internal [[Class]] property, if it doesn't 
        // match the `HTMLUnknownElement` interface than it must be supported
        return toString.call(element) !== '[object HTMLUnknownElement]';
    };

})(this);


来源:https://stackoverflow.com/questions/7048102/check-if-html-element-is-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!