【问题标题】:Select all the table cells, but not the embedded table's cells选择所有表格单元格,但不选择嵌入表格的单元格
【发布时间】:2011-05-02 18:39:29
【问题描述】:

如何选择表格中的单元格而不是嵌入表格的单元格? JQuery 中有一个关于如何执行此操作的问题。我需要在 CSS 中执行此操作。

<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>

【问题讨论】:

    标签: css selector


    【解决方案1】:

    您可以使用 >,子选择器。
    示例:

    table#Outer > tbody > tr > td { color: red; }
    

    子选择器只选择直接后代。有关子选择器的更多信息:http://meyerweb.com/eric/articles/webrev/200006b.html。但并非所有网络浏览器都支持它:http://www.quirksmode.org/css/contents.html

    【讨论】:

    • 完全没有问题(和一个 +1,用于提供 css 解决方案,按照要求...而不是我的 jQuery-vision-provided 解决方案...叹息)=)
    • 哈哈 - 当我们找到一个可以回答的问题时,我们**所有人**都会感到兴奋。 ;)
    • 是的,我们确实...但是在 16k 时,我真的应该度过最初的兴奋... XD
    【解决方案2】:

    正如链接问题中的评论所给出的:

    table#Outer > tbody > tr > td {  }
    

    请注意,由于级联,除非您为所有单元格提供默认覆盖样式,否则更改也将应用于内部表格:

    td { background-color: white; }
    table#Outer > tbody > tr > td { background-color:red; }
    

    http://jsfiddle.net/95NAd/

    【讨论】: