开发者

Is there a better way to write this in jQuery?

$(".sectionHeader").click(function() {
    var row = $开发者_运维技巧(this).parent();
    while (row.next().length != 0) {
        row = row.next();
        if (row.children().first().is('th')) return;
        row.toggle();
    }
});


It looks like you have rows in the table that are section headers, in that case, you can collapse until the next sectionHeader like this:

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

You could make it more efficient with delegate() like this:

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

This attaches one event handler for the entire table instead of 1 per .sectionHeader row.


$(".sectionHeader").click(function() {
    $(this).parent().each(function(index, element){
      if($(element).children().first().is('th')) return;
      $(element).toogle();
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜