css - 选择父元素中 3 的倍数的元素

标签 css css-selectors

有没有办法用 css 选择父元素中索引倍数为 6 的元素?

例如,在这种情况下,我只想选择 3 的倍数:

<div>
    <p></p>
    <p></p>
    <p></p> <!--to select -->
    <p></p>
    <p></p>
    <p></p> <!--to select -->
    <p></p>
    <p></p>
    <p></p> <!--to select -->
</div>

最佳答案

使用:nth-child(n) :

p:nth-child(3n) {
  background: red
}

演示:http://jsbin.com/azehum/4/edit

此方法适用于 IE9+(来源:caniuse.com)。如果您需要旧版浏览器的支持,您可以使用 jQuery选择元素并向它们添加类:

$("p:nth-child(3n)").addClass("redbg");

关于css - 选择父元素中 3 的倍数的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10639587/

相关文章:

html - 绝对定位100%高度滚动溢出问题

HTML - 在新行中放置图像

CSS3 :after pseudo element with input

css - Element.but 选择器 CSS

jquery - 为什么 ":last"和 ":last-child"不起作用?

javascript - 如何从单独的 jsx 文件设置标题的背景图像?

html - 图像上的斜带 css

internet-explorer - 如何在 IE9 和 Firefox 中应用 appearance none 属性?

jquery - CSS优先级重要吗?

html - 如何使用CSS排除某些特定元素的样式?