本文介绍了如何选择表单元格,而无需在jQuery中选择嵌套表单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想选择表中的'td'元素的第一级,而不是任何嵌套表的单元格。
例如:
I want to select only the first level of 'td' elements in a table and not the cells of any nested tables.eg:
<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 ...)
(and yes in prod code i would include tbody, thead...)
推荐答案
我会使用选择器,它只选择与表达式匹配的直接子项。为了方便选择外表,我给它一个名字。 注意:此选项不适用于您在adwords,tbody和tfoot的选择器中添加的示例,因为您表示您将在生产中。
I'd use the children selector, which only selects the immediate children matching the expression. To make it easy to select just the outer table, I'd give it a name. NOTE: this won't work with your sample as I've added in the selectors for thead, tbody, and tfoot as you indicate you will have in production. Adjust accordingly for your sample.
$('table#namedTable').children('tbody,thead,tfoot')
.children('tr')
.children('td')
这篇关于如何选择表单元格,而无需在jQuery中选择嵌套表单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!