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

时间:2009-04-21 21:48:01

标签: 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>

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

2 个答案:

答案 0 :(得分:10)

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

$('table#namedTable').children('tbody,thead,tfoot')
                     .children('tr')
                     .children('td')

答案 1 :(得分:0)

给最外面的表一个id为“Outer”:

$("table#Outer > td").text("selected");