在 CSS 中结合 :last-child 和 :not(.class) 选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8300185/
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 :last-child with :not(.class) selector in CSS
提问by dcastro
Is it possible to choose the last child that does NOT have a class attr? Like such:
是否可以选择最后一个没有班级属性的孩子?像这样:
<tr> ... </tr>
<tr> ... </tr>
<tr> ... </tr>
<tr class="table_vert_controls"> ... </tr>
I want to select the third row, in this case.
在这种情况下,我想选择第三行。
I tried the following, which did not work:
我尝试了以下方法,但没有用:
tr:not(.table_vert_controls):last-child
Thanks in advance.
提前致谢。
采纳答案by BoltClock
Not with CSS selectors alone, no, as :last-child
specifically looks at the last child, and there isn't a similar :last-of-class
pseudo-class. See my answer to this related question.
不单独使用 CSS 选择器,不,因为:last-child
专门查看last child,并且没有类似的:last-of-class
伪类。请参阅我对这个相关问题的回答。
As of late 2015, implementations have begun slowly trickling in for Selectors 4's extension to :nth-child()
and :nth-last-child()
which allow you to pass an arbitrary selector as an argument (more on that also in an update to the linked answer). You will then be able to write the following:
到 2015 年末,Selectors 4 的扩展开始慢慢地实现,:nth-child()
并且:nth-last-child()
允许您将任意选择器作为参数传递(更多相关信息也在链接答案的更新中)。然后,您将能够编写以下内容:
tr:nth-last-child(1 of :not(.table_vert_controls))
Although implementations have recently begun to ship, it will still take at least a few more years for this to be widely supported (as of April 2018, two and a half years after being the first browser to ship, Safari remains the only browser to have done so). In the meantime, you'll have to use something else, like an extra class just before the class in question, or a jQuery selector:
尽管最近开始发布实现,但至少还需要几年时间才能得到广泛支持(截至 2018 年 4 月,在成为第一个发布的浏览器两年半后,Safari 仍然是唯一拥有这样做)。同时,您必须使用其他东西,例如在相关类之前的额外类,或 jQuery 选择器:
$('tr:not(.table_vert_controls):last')
回答by user3882055
You can use the following solution:
您可以使用以下解决方案:
tr:not(.table_vert_controls):nth-last-of-type(2)