Php 使用jQuery隐藏与另一个div具有相同类的div?

Php 使用jQuery隐藏与另一个div具有相同类的div?,php,jquery,Php,Jquery,基本上,我希望通过jQuery完成以下工作: <div class="120 most-voted"> <!-- DON'T HIDE THIS DIV --> </div> <div class="110 voted"> <!-- some stuff here --> </div> <div class="120 voted">

基本上,我希望通过jQuery完成以下工作:

    <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函数动态生成:

    <?php $postid = get_the_ID(); // capture the id ?>

    <div id="<?php echo $postid; ?>" class="most-voted">


它不是有效的html,id在文档中必须是唯一的


使用rel属性而不是id。然后使用选择器,如:

$('div.voted[rel=110]')

如果您想使用相同的div,您应该使用class属性,因为不允许使用双id

 <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>


使用此按钮定位第一个,这将使类为“v120”的第一个div可见,所有其他div隐藏:

var theClass = "v120";
$("div." + theClass).first().show().end().not(":first").hide();

(我在“120”中添加了“v”,因为我不确定“120”是否是有效的CSS类。)

工作原理:

  • 用类-
    $()
    查找所有
    div
    s
  • 将该设置临时减少为第一个匹配项-
  • 那一个(以防它曾经被隐藏)
  • 结束集合的临时缩减-
  • 将集合减少为仅匹配不是第一个的项-
  • 他们

  • 您不应该有任何具有相同ID的元素。这就是
    ID
    属性的要点。如果您需要使用一组相关元素,请尝试使用类而不是ID。@Matt Gibson OK我将其更改为类。
    div
    没有
    rel
    属性:对于有(
    a
    区域
    链接
    )的元素,它定义了此文档与另一文档的关系,不是将此元素转换为其他元素或抽象概念,如“投票”。谢谢!但是当你说所有其他的时候,你指的是所有其他v120类的div,或者所有其他的div,不管是哪一类的div?我只想隐藏第二个具有v120类的div。@alexchenco:所有其他具有v120类的div。更新了示例以演示。@alexchenco:还添加了解释。:-)