jQuery - 抓住所有next()达到一定数量

时间:2012-01-31 05:39:58

标签: jquery html-table tablerow

好的,基本上,我有一个恰好是<tr>元素的元素。

但如果我找到行数大于1的<td>元素,则在任何<tr>元素中,我需要排除这些<tr>元素,从$开始(“td”)。parent()其中rowspan&gt; 1,我需要排除所有其他<tr>元素,直到each()

中的rowspan数量

这样的事情:

$("td").each(function{
if ($(this).attr('rowspan') > 1)
    var curRowspan = $(this).attr('rowspan');
    // So now rowspan can equal 2, 3, 4, 5, and higher.
    // Now I need to exclude the NEXT <tr> elements based on the quantity of rowspans.
    // So if the rowspan = 2, than I need to exclude $(this).parent() and $(this).parent().next() which seems easy enough, but I need this to work on more than 2 rowspans also.  Needs to exclude the current <tr> element and all <tr> elements after, until it reaches the rowspan quantity indicated by curRowspan.
    // HOW TO DO THIS and return false out of the each() for each of these `<td>` elements within that quantity of `<tr>` elements indicated by the rowspan of a `<td>` element???
});

1 个答案:

答案 0 :(得分:0)

您可以添加一个类来表示您忽略该行。我觉得有一种更好的方法来优化这个选择器。 Slice看起来很有希望。

http://jsfiddle.net/UG35Q/2/

$("td").each(function() {
    if ($(this).attr('rowspan') > 1) {        
        var curRowspan = $(this).attr('rowspan');
        $(this).parent().nextAll().slice(0, curRowspan).addClass('markRow');
    }
});