我正在使用
Ublock Origin
浏览器扩展程序,通过将 css 规则注入 user agent stylesheet
来隐藏广告:
a[href^="https://ad.doubleclick.net/"] {
display: none !important;
}
即使有样式属性,它也会隐藏链接:
there should be a link:
<a href="https://ad.doubleclick.net/" style="display: block !important">example1</a>
即使 devtools 显示 style 属性应该覆盖该规则:
“用户样式表”中类似的CSS规则可以被样式属性覆盖,没有问题:
a[href^="https://example.net/"] {
display: none !important;
}
there should be a link:
<a href="https://example.net/" style="display: block !important">example2</a>
这是浏览器错误吗? 有解决办法吗?
Edge 119.0.2151.44
Windows 10 x64
观察到的行为似乎遵循 https://developer.mozilla.org/en-US/docs/Web/CSS/Cascade#cascading_order 概述的层次结构。
具有重要性
!important
的起源“作者(开发者)”按照这些事物优先级的(升序)顺序位于位置 5,而带有 !important
的“用户代理(浏览器)”位于位置 7,并且因此否决了作者的样式表。
所以浏览器的解释在这里似乎是正确的;并且错误将出现在开发工具方面,它不能正确地“可视化”这一点。