在 CSS 中组合 :not() 选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7403129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combining :not() selectors in CSS
提问by pimvdb
I'm trying to select all tr
elements inside a table
, except the third and fourth. I managed to do so by using:
我试图选择 a 中的所有tr
元素table
,除了第三个和第四个。我设法通过使用:
#table tr:not(:nth-child(3)):not(:nth-child(4))
I'd like to combine those selectors because I've some more :nth-child
entries. Something like this, but it didn't work:
我想组合这些选择器,因为我有更多的:nth-child
条目。像这样的东西,但它不起作用:
#table tr:not(:nth-child(3), :nth-child(4))
This doeswork in jQuery, but not in CSS. I'm using Chrome (and I only need it to work there).
这确实工作jQuery的,但不是在CSS。我正在使用 Chrome(我只需要它在那里工作)。
I've not been able to find a combining selector for this case. How can I combine selectors with :not
?
我一直无法为这种情况找到组合选择器。如何将选择器与:not
?
回答by BoltClock
Selectors level 3 does not allow anything more than a single simple selectorwithin a :not()
pseudo-class. As a jQuery selector, it works because jQuery extends its :not()
functionality to allow any selector(like the .not()
method).
选择器级别 3 只允许在伪类中使用一个简单的选择器:not()
。作为一个 jQuery 选择器,它之所以有效是因为 jQuery 扩展了它的:not()
功能以允许任何选择器(如.not()
方法)。
However, your second syntax is one of the proposed enhancements to :not()
in Selectors 4, and works equivalently to your first. Although the example (shown as of this writing anyway) shows a chain of :not()
selectors, the proposal says:
但是,您的第二个语法是Selectors 4 中建议的增强功能:not()
之一,并且与您的第一个语法等效。尽管示例(在撰写本文时显示)显示了一个:not()
选择器链,但提案说:
The negation pseudo-class, :not(X), is a functional notation taking a selector list as an argument. It represents an element that is not represented by its argument.
否定伪类 :not(X) 是一个函数符号,将选择器列表作为参数。它代表了一个没有被它的参数所代表的元素。
Here a selector list is simply a comma-separated list of selectors.
这里的选择器列表只是一个逗号分隔的选择器列表。
If you need to negate selectors that contain combinators (>
, +
, ~
, space, etc, for example div p
), you can't use :not()
in CSS; you'll have to go with the jQuery solution.
如果您需要否定包含组合符(例如>
、+
、~
、 空格等div p
)的选择器,则不能:not()
在 CSS 中使用;你必须使用 jQuery 解决方案。
回答by OZZIE
Although the marked answer is true I just want to point out that you can achieve what you want using css also but just in a different way. Instead of using not you can select those and unapply the stuff you don't want.
尽管标记的答案是正确的,但我只想指出,您也可以使用 css 实现您想要的功能,但只是以不同的方式。您可以选择那些并取消应用您不想要的东西,而不是使用 not。
#table tr {
/* general case styling */
color: blue;
}
#table tr:nth-child(3),
#table tr:nth-child(4) {
color: black;
}