我只想选择表格中第一级“td”元素,而不是任何嵌套表格的单元格。 例如:
<table id="Outer">
<tr>
<td> --this one
</td>
<td> --this one
<table>
<tr>
<td></td> -- but not this one or any deeper nested cells
</tr>
</table>
</td>
</tr>
</table>
(是的,在产品代码中我会包括 tbody、thead...)
最佳答案
我会使用children选择器,仅选择与表达式匹配的直接子级。为了便于仅选择外表,我会给它一个名称。 注意:这不适用于您的示例,因为我已在 thead、tbody 和 tfoot 的选择器中添加了您指出的生产中的内容。根据您的样本进行相应调整。
$('table#namedTable').children('tbody,thead,tfoot')
.children('tr')
.children('td')
关于jquery - 如何在 jQuery 中选择表格单元格而不选择嵌套表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/774774/