我有几个div
元素,我想在其中替换另一组div
个样式。因此,基本上将孩子的风格改为交替的背景颜色,如下所示:
HTML 的
<article class="post"> <!--first post-->
<div class="title">Title Here</div>
content here
</article>
<article class="post"> <!--second post-->
<div class="title">Title Here</div>
content here
</article>
CSS
div.title:nth-of-type(even) {
background-color: #F00;
}
div.title:nth-of-type(odd) {
background-color:#00F;
}
有没有办法做到这一点,因为我知道使用css
替换样式它必须在父级内。或者,如果没有,我可以使用任何jquery
脚本吗?
谢谢。
答案 0 :(得分:1)
nth-of-type
总是在检查父母中元素的位置。因此,您的div's
始终是.post
的第一个孩子。这就是它无法运作的原因。
但你可以查看它的父母的孩子位置。就像那样:
.post:nth-of-type(even) div.title{}
.post:nth-of-type(odd) div.title{}
答案 1 :(得分:1)