gpt4 book ai didi

javascript - 检查 CSSRule 是否匹配元素

转载 作者:行者123 更新时间:2023-11-30 13:14:42 30 4
gpt4 key购买 nike

有没有办法检查 CSS 选择器(通过 document.styleSheets[666].rules[1984].selectorText 获得)是否匹配特定元素?

我要求一个跨浏览器的纯 javascript 解决方案,但如果只是在其中完成,我会考虑使用 jQuery。

最佳答案

对于现代浏览器,有 matchesSelector .对于不太现代的浏览器,您可以使用 querySelectorAllwalk the DOM .

不要使用 jQuery。 Sizzle就够了。

Sizzle.matchesSelector(element, selector)

https://github.com/jquery/sizzle/wiki/Sizzle-Documentation

关于javascript - 检查 CSSRule 是否匹配元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12471421/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com