代码之家  ›  专栏  ›  技术社区  ›  seedg

使用jquery链接具有相同计数器的div

  •  0
  • seedg  · 技术社区  · 14 年前

    我有一个使用PHP从数据库中获取数据的表。

    该表包含父级2个元素,假设它们是:

    Name    Surname    << table row id = info1
    
    Name    Surname    << table row id = info2
    

    在每一行下面,都有一行默认隐藏。单击名字时,这是我希望它显示的方式:

    Name    Surname    << table row id = info1
    DOB     City       << table row id = infoSub1
    
    Name    Surname    << table row id = info2
                       << table row id = infoSub2 NOT DISPLAYED
    

    我如何才能以最有效的方式实现这一点?

    谢谢

    2 回复  |  直到 14 年前
        1
  •  0
  •   benhowdle89    14 年前
    $("#info1").click(function() {
        $(this).children("#infoSub1").slideDown();
    });
    

    然后对info2和infoSub2执行相同的操作:)

        2
  •  0
  •   Nick Craver    14 年前

    如果可以给父行一个类(例如 parent ,然后您可以使用 .nextUntil() :

    $("tr.parent").click(function() {
      $(this).nextUntil(".parent").toggle();
    });
    

    这只会选择所有 <tr> 元素 不是 .parent 跟踪单击的行,直到找到一个。这允许您在中间有任意数量的非父行,并且它将正确地切换它们。如果您有一张有很多行的长桌,请使用 .delegate() 以类似的方式:

    $("#tableID").delegate("tr.parent", "click", function() {
      $(this).nextUntil(".parent").toggle();
    });
    

    它的行为相同,但会将一个事件处理程序附加到 <table> (而不是每个家长一个) <TR & GT; )