jquery get the last tr with a th

2019-08-30 19:25发布

using jquery how do alter the css of the last tr with a th

$("#mytable tr").has('th').last().parents('tr').css({'color':'blue'});

HTML

<table border="1" id="mytable">
    <tr>
        <th>row 1, cell 1</th>
        <th>row 1, cell 2</th> 
    </tr>
    <tr> 
        <th>row 1, cell 1 - change this</th>
        <th>row 1, cell 2 - change this</th> 
    </tr>
    <tr>
        <td>row 2, cell 1</td>
        <td>row 2, cell 2</td>
    </tr>
</table>

2条回答
虎瘦雄心在
2楼-- · 2019-08-30 19:34

As an alternative to xdazz's answer, I'd offer:

$('tr:has(th):last').css('color','blue');

References:

查看更多
ら.Afraid
3楼-- · 2019-08-30 19:44

Find the last th, and its parent should be the last tr who has th.

$("#mytable th:last").parent().css('color', 'blue');
查看更多
登录 后发表回答