Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery使用th获取最后一个tr_Javascript_Jquery_Jquery Selectors - Fatal编程技术网

Javascript jquery使用th获取最后一个tr

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>

使用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>
    <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');
参考资料:


如果这样做会发生什么?你希望发生什么?+1我从来没有真正理解过引号在jQuery中的
:has()
中是否有任何特殊意义(我知道它们在
:not()
中是无效的);我倾向于包含它们,只是因为我觉得它是对的。我觉得它是错的,因为你传递的是选择器而不是字符串:/has()上有引号,而不是
:not()
对我来说似乎也不一致……而且被编辑了(查看了,其中暗示引号是不必要的;但没有明确说明任何一种方式。显然,
:not()
:has()
中都允许使用引号。我发布了一个问题,询问原因: