【发布时间】:2017-05-23 13:44:45
【问题描述】:
我需要一个 css 选择器,用于 <table> 中的第一个 <tr>。
我在互联网上搜索并获得了一些选择器,例如 tr:first-child table *:first-child tr:first-child, table tr:first-child。但它们不适用于我的情况......
这是一段需要处理的HTML代码:
<table>
<thead>
<tr><th>you should chose this row</th></tr>
<tr><th>not this one</th></tr>
</thead>
<tbody>
<tr><td>not this one</td></tr>
</tbody>
</table>
<table>
<colgroup>
<col></col>
</colgroup>
<thead>
<tr><th>you should chose this row</th></tr>
<tr><th>not this one</th></tr>
</thead>
<tbody>
<tr><td>not this line</td></tr>
</tbody>
</table>
<table>
<colgroup>
<col></col>
</colgroup>
<tbody>
<tr><td>you should chose this row</td></tr>
<tr><th>not this one</th></tr>
</tbody>
</table>
(更喜欢没有 javascript 的解决方案)
【问题讨论】:
-
所有的孩子中都没有第一个孩子的 css 选择器。然而,我想说(不知道您的确切用例)可能存在语义上的考虑,这将导致您放弃
<thead>和<tbody>区分。如果您需要第一个<td>的样式,这对我来说意味着该行在某种程度上具有相同的含义,这与您将它们放在<thead>和<tbody>中的事实相冲突。我建议你选择<thead>或<tbody>,因为它们需要相同的样式,它们也必须具有相似的含义。
标签: html css css-selectors html-table