CSS nth-child - 一次获得第一个、第二个、第四个和第五个 child ?

标签 css css-selectors

对于 CSS3,我知道我可以使用 nth-child(an + b) 选择器。我有一个包含 5 行的表,我想用一个语句选择第 1、2、4 和 5 行。这是可能的还是我必须至少使用两个这样的语句:

:nth-child(-n+2)  /* gets 1 and 2 */
:nth-child(n+4)   /* gets 4 and 5 */

我希望使用像 nth-child(1,2,4,5)nth-child(1-2,4-5) 这样的语法,但是它似乎不是规范的一部分。

(我无法控制页面,这些行上没有 ID/类。)

最佳答案

如果你想明确一点,一种方法是像这样重复 :nth-child():

tr:nth-child(1), tr:nth-child(2), tr:nth-child(4), tr:nth-child(5)

或者,由于表格恰好有 5 行,您可以只排除第三行,您将得到行 #1、#2、#4 和 #5:

tr:not(:nth-child(3))

jsFiddle preview

关于CSS nth-child - 一次获得第一个、第二个、第四个和第五个 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5160008/

相关文章:

javascript - 使用 Node JS 发送电子邮件

html - 滚动时页脚不粘在页面底部

css -::和 ~ 在 css 中做什么?

python - 使用 Selenium Python 通过值属性查找元素

java - JavaFX 中的样式按下按钮

html - 制作 :after the same size as its element

css - 如何做一个wordpress链接 block ?

html - 在复选框 hack 中规避 id 选择器

css-selectors - Jquery持久CSS选择器相当于 '.live()'

javascript - iOS8 Safari pushState 后出现:nth-child() selectors not works