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

标签 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...)

最佳答案

我会使用children选择器,仅选择与表达式匹配的直接子级。为了便于仅选择外表,我会给它一个名称。 注意:这不适用于您的示例,因为我已在 thead、tbody 和 tfoot 的选择器中添加了您指出的生产中的内容。根据您的样本进行相应调整。

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

关于jquery - 如何在 jQuery 中选择表格单元格而不选择嵌套表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/774774/

相关文章:

javascript - firebug 显示控制台打印文本的计数

php - jQuery UI 可排序 php/mysql

jquery - Contact Form 7 在提交时总是重定向到同一页面

javascript - 选择未包含在特定元素中的所有元素

javascript - jQuery - 查找其中包含多个 HTML 标记属性的元素

javascript - jQuery JSON 发布 : Invalid object passed in, ':' 或 '}' 预期

CSS :not style outside element

css - 为具有子元素的 <li> 设置样式

javascript - 为什么要使用诸如:not() and :has() allow quoted arguments?这样的伪函数