【发布时间】:2012-10-26 13:02:22
【问题描述】:
我有几个左浮动的项目 div,因此它们看起来在列中。用户可以单击任何给定项目上的“隐藏”,并为该元素添加一个“.hide”类,该元素具有 display:none css。
问题:有没有办法做到.item:not(.hide):nth-child(2n+2)?因此,当我有一个项目列表,其中一个项目的中间有 .hide 时,该规则仍然适用并“忽略”.hide 项目。
jsbin 中的实例,http://jsbin.com/ajeqal/edit#javascript,html,live
如果您在右侧列项中单击“隐藏”,则列会出现乱序,因为左侧列应该始终具有与右侧列不同的背景颜色。我可以将隐藏的项目移到列表的末尾,但这需要更多的工作。
HTML:
<style>
.hide { display:none;}
.item{ float: left; width: 150px; padding: 5px; background: #ccc;}
.item:not(.hide):nth-child(2n+1) { background: #999; margin-right:5px }
.item a { display:inline-block; margin-left: 10px; }
body { width: 325px; }
#unhideAll { display: none; clear: left; margin-top: 20px;}
#container { overflow: auto; }
</style>
<body>
<div id="container">
<div class="item">item<a href="#">hide</a></div>
<div class="item">item<a href="#">hide</a></div>
<div class="item hide">item<a href="#">hide</a></div>
<div class="item">item<a href="#">hide</a></div>
</div>
<div id="unhideAll"><a href="#">Unhide All</a></div>
</body>
Js:
$('#unhideAll').click(function(e) {
e.preventDefault();
$(".hide").removeClass("hide");
$(this).hide();
});
$(".item a").click(function(e) {
e.preventDefault();
$(this).parent().addClass("hide");
$("#unhideAll").show();
});
【问题讨论】:
-
$('.item:nth-child(2n+2)').not($('.hide')) -
谢谢,但我希望在 CSS 中有一个规则来为我做这件事
-
不幸的是,没有这样的规则。 CSS
:nth-child()不会这样过滤。 -
没有。
nth-child不与其他选择器结合使用..
标签: jquery css pseudo-class