Select odd even child excluding the hidden child(选择排除隐藏孩子的奇偶孩子)
问题描述
第 3 行是隐藏的 <div> .我不希望从 odd/even css 规则中获取那个.
Line 3 is a hidden <div> . I don't want that one to be taken from the odd/even css rule.
实现此功能的最佳方法是什么?
What is the best approach to get this to work?
.hidden {display:none;}
.box:not(.hidden):nth-child(odd) { background: orange; }
.box:not(.hidden):nth-child(even) { background: green; }
<div class="wrap">
<div class="box">1</div>
<div class="box">2</div>
<div class="box hidden">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
</div>
http://jsfiddle.net/k0wzoweh/
注意:隐藏元素可以有多个.
Note: There can be multiple hidden elements.
推荐答案
伪选择器不会堆叠,所以你的 :not 不会影响 :nth-child (也不会影响 :nth-of-type 等
Pseudo-selectors don't stack, so your :not doesn't affect the :nth-child (nor would it affect :nth-of-type etc.
如果你可以使用 jQuery,你可以在那里使用 :visible 伪选择器,尽管这不是 CSS 规范的一部分.
If you can resort to jQuery, you can use the :visible pseudo-selector there, although that's not a part of the CSS spec.
如果您正在生成 HTML 并且可以更改它,您可以在运行时应用奇数/偶数逻辑,例如在 PHP 中:
If you're generating the HTML and can change that, you can apply odd/even with logic at run-time, eg in PHP:
foreach ($divs AS $i => $div) {
echo '<div class="box ' . ($i % 2 ? 'even' : 'odd') . '">x</div>';
}
甚至尝试做一些棘手的事情,比如
Even trying to do something tricky like
.box[class='box']:nth-of-type(even)
不起作用,因为伪选择器甚至不会堆叠到属性选择器上.
doesn't work, because the psuedo-selector doesn't even stack onto the attribute selector.
我不确定是否有任何方法可以纯粹使用 CSS 来做到这一点 - 我现在想不出任何方法.
I'm not sure there's any way to do this purely with CSS - I can't think of any right now.
这篇关于选择排除隐藏孩子的奇偶孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:选择排除隐藏孩子的奇偶孩子
基础教程推荐
- 每次设置弹出窗口的焦点 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
