我一直在使用 :not()
伪类来设置样式,而不需要用第二个不必要的声明覆盖它来撤消第一个,
但现在我遇到了一个奇怪的行为,即 Safari 接受 :not()
中的后代选择器,但 Chrome 不接受。
我使用了类似a:not(.blue a)
的东西。
我搜索了答案,但我仍然没有完全理解原因。
规范真的允许后代选择器吗?
这是一个演示:
a:not(.blue a) {
color: red;
}
<div><a>this one should be in red</a></div>
<div class="blue"><a>this one shouldn't</a></div>
http://codepen.io/oscarmarcelo/pen/YqboQJ?editors=1100
在Selectors Level 3 ,答案是否定的。 :not()
表示法只接受简单的选择器。
6.6.7. The negation
pseudo-class
The negation pseudo-class, :not(X)
, is a functional notation taking
a simple selector (excluding the negation pseudo-class itself) as an
argument. It represents an element that is not represented by its
argument.
什么是简单选择器?
来自 selector syntax :
A simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.
与后代选择器无关。
然而,在Selectors Level 4 , :not()
接受复杂选择器,其中包括后代组合器。浏览器对该规范的支持仍然很弱。