This

JQuery to select the last cell in a column when using rowspan

I have a table like this:

<table>
    <tr><td>Not This</td><td rowspan="3">This</td></tr>
    <tr><td>Not This</td></tr>
    <tr><td>Not This</td></tr>
    <tr><td>Not This</td><td>This</td></tr>
</table>

How can I select only the rightmost cells (containing "This") in each row to set the border color?

I tried something like:

table.find('tr > td:last-child').addClass('someclass');

But this selects the last cells in the 2nd and 3rd rows, even if they are not the rightmost cell.

I do not use border-collapse on my table and prefer to avoid this.

+3
source share
4 answers

This required a bit of cheating:

$(function() {
    $('td:last-child[rowspan]').each(function() {
        $(this).parent().nextAll().slice(0,$(this).attr('rowspan')-1).addClass('skip');
    });
    $('tr:not(.skip) > td:last-child').addClass('someclass');
    $('.skip').removeClass('skip');
});

, td, , rowspan. , , "" . , "skip", , , .

: http://jsfiddle.net/Ender/rzqEr/

+3

- , , , :

$('table tr').each(function(){

  if( $('td',this).size() > 1 ){
    $(this).find('td:last-child').addClass('someclass');
  };

});

JSBin

, Spolto:

$('table tr').each(function(){

  $('td:gt(0)',this).addClass('someclass');

});

JSBin

+1

<td> :

$("table tr td:gt(0)").addClass('someclass');
+1

This should work:

$("TD ~ TD:last-child").addClass('someclass');

Setting the border on the table itself will probably work too.

0
source

Source: https://habr.com/ru/post/1779482/


All Articles