"和children()-6ren"> "和children()-HTML 代码: The first row The first row The second row The second row -6ren">
gpt4 book ai didi

jquery:如何在表中使用 ">"和children()

转载 作者:行者123 更新时间:2023-12-01 00:02:34 25 4
gpt4 key购买 nike

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>

jQuery 代码:

$(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()”选择器不起作用,为什么?

工作示例: https://jsfiddle.net/6knk67gd/1/

我已经检查了这两个选择器的用法,但仍然在我的代码中找不到任何问题。请告诉我如何正确使用,谢谢~

最佳答案

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

尝试

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

关于jquery:如何在表中使用 ">"和children(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31531342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com