我有几个带有浮动的项目 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();
});