How to detect if browser support specified css pseudo-class?

允我心安 提交于 2019-12-04 06:25:46

You can simply check if your style with pseudo-class was applied.

Something like this: http://jsfiddle.net/qPmT2/1/

stylesheet.insertRule(rule, index) method will throw error if the rule is invalid. so we can use it.

var supportPseudo = function (){
    var ss = document.styleSheets[0];
    if(!ss){
        var el = document.createElement('style');
        document.head.appendChild(el);
        ss = document.styleSheets[0];
        document.head.removeChild(el);
    }
    return function (pseudoClass){
        try{
            if(!(/^:/).test(pseudoClass)){
                pseudoClass = ':'+pseudoClass;
            }
            ss.insertRule('html'+pseudoClass+'{}',0);
            ss.deleteRule(0);
            return true;
        }catch(e){
            return false;
        }
    };
}();


//test
supportPseudo(':hover'); //true
supportPseudo(':before'); //true
supportPseudo(':hello'); //false
supportPseudo(':world'); //false

If you're OK with using Javascript, you might skip the detection and go right for the shim: Selectivizr

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