jquery用th获得最后一个tr

Hel*_*rld 0 javascript jquery jquery-selectors

使用jquery如何用th改变最后一个tr的css

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

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>
Run Code Online (Sandbox Code Playgroud)

Dav*_*mas 6

作为xdazz答案的替代方案,我提供:

$('tr:has(th):last').css('color','blue');
Run Code Online (Sandbox Code Playgroud)

参考文献:


xda*_*azz 5

找到最后th,与其父应该是最后tr谁拥有th.

$("#mytable th:last").parent().css('color', 'blue');
Run Code Online (Sandbox Code Playgroud)