【问题标题】:Is there a better way to write this in jQuery?有没有更好的方法在 jQuery 中编写这个?
【发布时间】:2010-03-26 19:30:59
【问题描述】:
$(".sectionHeader").click(function() {
    var row = $(this).parent();
    while (row.next().length != 0) {
        row = row.next();
        if (row.children().first().is('th')) return;
        row.toggle();
    }
});

【问题讨论】:

    标签: jquery-ui jquery jquery-selectors


    【解决方案1】:

    看起来你的表格中有行是节标题,在这种情况下,你可以像这样折叠直到下一个 sectionHeader:

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

    您可以像这样使用delegate() 提高效率:

    $("#sectionTable").delegate(".sectionHeader", "click", function() {
        $(this).closest("tr").nextUntil(".sectionHeader").toggle();
    });
    

    这会为整个表附加一个事件处理程序,而不是每个.sectionHeader 行附加一个。

    【讨论】:

      【解决方案2】:
      $(".sectionHeader").click(function() {
          $(this).parent().each(function(index, element){
            if($(element).children().first().is('th')) return;
            $(element).toogle();
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多