使用jQuery隐藏与另一个类相同的div?
基本上,我想完成与jQuery如下:使用jQuery隐藏与另一个类相同的div?
<div class="120 most-voted">
<!-- DON'T HIDE THIS DIV -->
</div>
<div class="110 voted">
<!-- some stuff here -->
</div>
<div class="120 voted">
<!-- hide this div with class '120' since there's already
another div with class '120' at the top -->
</div>
<div class="23 voted">
<!-- some stuff here -->
</div>
编辑:
<?php $postid = get_the_ID(); // capture the id ?>
<div id="<?php echo $postid; ?>" class="most-voted">
我不想隐瞒:数字是由PHP函数动态生成div顶部。
任何建议(我不介意在顶部的div中包装另一个div来完成此结果)?
这将使第一个div带班 “V120” 看得见和所有其他隐藏:
var theClass = "v120";
$("div." + theClass).first().show().end().not(":first").hide();
(我因为我不确定“120”是否是有效的CSS类,所以将“v”添加到“120”。)
如何工作的:
- 找到所有
div
s的类 -$()
。 - 暂时减少该集合到第一匹配 -
.first()
-
show
之一(在情况下,它用于被隐藏) - 结束该组的临时还原 -
.end()
- 降低设定到只有匹配这不第一 -
.not(":first")
-
hide
他们
谢谢!但是当你说所有其他的时候,你指的是所有其他的v120级别的div,或者所有其他divs,而不管这个级别是什么?我只想隐藏类v120的第二个div。 – alexchenco 2011-03-27 08:33:16
@alexchenco:所有其他div类与“v120”类。更新了示例以证明这一点。 – 2011-03-27 08:34:22
感谢这个例子。 – alexchenco 2011-03-27 08:36:56
使用rel属性,而不是ID的范围内是唯一的。然后使用选择这样的:如果你想使用同一个div
$('div.voted[rel=110]')
'div'没有'rel'属性:http://www.w3.org/TR/html5/grouping-content.html#the-div-element对于('a',''area ''''link'),它定义了这个* document *与另一个* document *的关系,而不是这个元素与另一个元素或者像“votes”这样的抽象概念。 – 2011-03-27 08:32:52
,你应该因为双ID不允许conceder使用class属性。
<div class="most-voted id_120">
<!-- some stuff here -->
</div>
<div class="voted id_110">
<!-- some stuff here -->
</div>
<div class="voted id_120">
<!-- hide this div with id 120 since its already
at the top in the div with class most-voted -->
</div>
<div class="voted id_100">
<!-- some stuff here -->
</div>
请看我的编辑,对不起。 – alexchenco 2011-03-27 08:23:22
@alexchenco我编辑我的帖子在底部 – fatnjazzy 2011-03-27 08:25:48
你不应该有* *任何元素有s ame ID。这就是'id'属性的要点。如果你需要用一堆相关的元素来做事,可以尝试使用一个类而不是一个ID。 – 2011-03-27 08:18:49
@Matt Gibson好的,我把它改成了一堂课。 – alexchenco 2011-03-27 08:20:53
这个ID是什么意思? – Marcel 2011-03-27 08:23:16