jquery:如何使用&燃气轮机&引用;和表中的儿童()

jquery:如何使用&燃气轮机&引用;和表中的儿童(),jquery,jquery-selectors,parent-child,Jquery,Jquery Selectors,Parent Child,Html代码: <table> <tr> <td>The first row</td> <td>The first row</td> </tr> <tr> <td>The second row</td> <td>The second row</td> </tr> <

Html代码:

<table>
    <tr>
        <td>The first row</td> <td>The first row</td>
    </tr>
    <tr>
        <td>The second row</td> <td>The second row</td>
    </tr>
    <tr>
        <td>The third row</td> <td>The third row</td>
    </tr>
    <tr>
        <td>The forth row</td> <td>The forth row</td>
    </tr>
</table>
<hr>
<table>
    <tr>
        <td>The first row</td> <td>The first row</td>
    </tr>
    <tr>
        <td>The second row</td> <td>The second row</td>
    </tr>
    <tr>
        <td>The third row</td> <td>The third row</td>
    </tr>
    <tr>
        <td>The forth row</td> <td>The forth row</td>
    </tr>
</table>
$(function () {
    $("table:first tr").css("background", "#ffbbbb");   //work
    $("table:last>tr").css("background", "#ffbbbb");   //not work
    $("table:last").children("tr").css("background", "#ffbbbb");  //not work
});
结果:第一个表的背景已更改,但第二个表未更改。 空间选择器似乎起作用了,但'>'和'children()'选择器没有,为什么?

工作示例:


我已经检查了这两个选择器的使用情况,但仍然没有在代码中发现任何问题。请告诉我如何正确使用它们,谢谢~

即使我们没有创建
tbody
,默认情况下dom结构将创建它,因此所有
tr
都将是
tbody/thead/tfooter
的子对象,而不是
表本身

试一试


符号>表示直接后代,在表和tr之间会自动生成一个tbody。请尝试以下操作:

$("table:last > tbody > tr").css("background", "#ffbbbb");

您刚刚错过了表和tbody之间的链接,因此Arun P Johny是正确的,您可以像那样应用css
$("table:last > tbody > tr").css("background", "#ffbbbb");