如何选择表格单元格而不在jQuery中选择嵌套表格单元格

mis*_*ghi 2 jquery css-selectors sizzle

我想只选择表中第一级'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>
Run Code Online (Sandbox Code Playgroud)

(在prod代码中是的,我会包括tbody,thead ...)

tva*_*son 10

我使用选择器,它只选择与表达式匹配的直接子项.为了便于选择外表,我给它起了一个名字. 注意:这不适用于您的样本,因为我已经添加了thead,tbody和tfoot的选择器,因为您指示您将在生产中.根据您的样品进行相应调整

$('table#namedTable').children('tbody,thead,tfoot')
                     .children('tr')
                     .children('td')
Run Code Online (Sandbox Code Playgroud)