如何检测浏览器是否支持指定的css伪类?

发布于 2024-12-21 10:24:27 字数 333 浏览 4 评论 0原文

通过 JavaScript 检测浏览器对任何 css 伪类的支持是什么概念?确切地说,我想检查用户的浏览器是否支持 :checked 伪类,因为我制作了一些带有复选框的 CSS 弹出窗口,并且需要对旧浏览器进行回退。

答案:我发现已经实现了在 Modernizr 中测试 css 选择器的方法 “其他测试”

What's concept of detecting support of any css pseudo-class in browser through JavaScript? Exactly, I want to check if user's browser supports :checked pseudo-class or not, because I've made some CSS-popups with checkboxes and needs to do fallbacks for old browsers.

ANSWER: I'm found already implemented method of testing css selectors in a Modernizr "Additional Tests".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

杀手六號 2024-12-28 10:24:27

对于仍在寻找此问题的快速解决方案的人,我根据本线程中的其他一些答案整理了一些内容。我的目标是使其简洁。

function supportsSelector (selector) {
  const style = document.createElement('style')
  document.head.appendChild(style)
  try {
    style.sheet.insertRule(selector + '{}', 0)
  } catch (e) {
    return false
  } finally {
    document.head.removeChild(style)
  }
  return true
}

supportsSelector(':hover') // true
supportsSelector(':fake') // false

For anyone still looking for a quick solution to this problem, I cribbed together something based on a few of the other answers in this thread. My goal was to make it succinct.

function supportsSelector (selector) {
  const style = document.createElement('style')
  document.head.appendChild(style)
  try {
    style.sheet.insertRule(selector + '{}', 0)
  } catch (e) {
    return false
  } finally {
    document.head.removeChild(style)
  }
  return true
}

supportsSelector(':hover') // true
supportsSelector(':fake') // false
嘦怹 2024-12-28 10:24:27

您可以简单地检查是否应用了带有伪类的样式。

像这样的东西: http://jsfiddle.net/qPmT2/1/

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

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

日记撕了你也走了 2024-12-28 10:24:27

stylesheet.insertRule(rule, index) 方法将如果规则无效,则抛出错误。所以我们可以使用它。

var support_pseudo = 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 (pseudo_class){
        try{
            if(!(/^:/).test(pseudo_class)){
                pseudo_class = ':'+pseudo_class;
            }
            ss.insertRule('html'+pseudo_class+'{}',0);
            ss.deleteRule(0);
            return true;
        }catch(e){
            return false;
        }
    };
}();


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

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

var support_pseudo = 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 (pseudo_class){
        try{
            if(!(/^:/).test(pseudo_class)){
                pseudo_class = ':'+pseudo_class;
            }
            ss.insertRule('html'+pseudo_class+'{}',0);
            ss.deleteRule(0);
            return true;
        }catch(e){
            return false;
        }
    };
}();


//test
support_pseudo(':hover'); //true
support_pseudo(':before'); //true
support_pseudo(':hello'); //false
support_pseudo(':world'); //false
活雷疯 2024-12-28 10:24:27

如果您可以使用 Javascript,则可以跳过检测并直接使用填充程序:Selectivizr

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文