Javascript jquery使用th获取最后一个tr
使用jquery如何使用一个Javascript jquery使用th获取最后一个tr,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,使用jquery如何使用一个 $("#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>
$("#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>
第1行,第1单元
第1行第2单元
第1行,单元格1-更改此设置
第1行,第2单元格-更改此设置
第2行第1单元
第2行,第2单元
查找最后一个th
,其父级应该是拥有th
的最后一个tr
$("#mytable th:last").parent().css('color', 'blue');
作为替代方案,我将提供:
$('tr:has(th):last').css('color','blue');
参考资料:
:has()
中是否有任何特殊意义(我知道它们在:not()
中是无效的);我倾向于包含它们,只是因为我觉得它是对的。我觉得它是错的,因为你传递的是选择器而不是字符串:/has()上有引号,而不是:not()
对我来说似乎也不一致……而且被编辑了(查看了,其中暗示引号是不必要的;但没有明确说明任何一种方式。显然,:not()
和:has()
中都允许使用引号。我发布了一个问题,询问原因: