我正在尝试对交替元素的颜色进行条纹处理。但我希望行颜色仅交替可见行。如果您查看下面的内容,这是我试图让它工作的尝试。
<!DOCTYPE html>
<html>
<head>
<style>
p:not(.hide):nth-child(odd)
{
background:#ff0000;
}
p:not(.hide):nth-child(even)
{
background:#0000ff;
}
.hide { display:none; }
</style>
</head>
<body>
<p>The first paragraph.</p>
<p class="hide">The second paragraph.</p>
<p>The third paragraph.</p>
</body>
</html>
最佳答案
你不能用纯 CSS 做到这一点,因为 :nth-child
选择器是根据元素计算的,而 :not
不会过滤元素在DOM。您需要使用 JavaScript 以获得完全灵活的解决方案。
你仍然可以不灵活地做到这一点,方法是让 .hide
之后的元素与 :nth-child
交替它们应该的颜色:
.hide + p:nth-child(odd) {
background: #0000ff;
}
对于越来越多的兄弟.hide
和p
的组合,你可以继续添加类似的规则,但这是非常不灵活的。
关于CSS3奇偶只可见行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15059388/