表“tabid”的第一行已按预期选择。然而,第二个表格的第一行也被选中,这是我不想要的。
table#tabid tr:first-of-type {
background-color: yellow;
}
<table id="tabid">
<tbody>
<tr>
<td>Select me</td>
<td>cell-112</td>
<td>cell-113</td>
</tr>
<tr>
<td>cell-121</td>
<td>cell-122</td>
<td>cell-123</td>
</tr>
<tr>
<td>cell-131</td>
<td>Cell-132</td>
<td>Cell-133</td>
</tr>
<tr>
<td>
<table>
<tbody>
<tr>
<td>, but not me</td>
<td>cell-141-1</td>
<td>cell-141-1</td>
<td>cell-141-1</td>
</tr>
</tbody>
</table>
</td>
<td>cell-142</td>
<td>cell-143</td>
</tr>
</tbody>
</table>
您可以使用 child combinator , >
像:
table#tabid > tbody > tr:first-of-type {
background-color: yellow;
}
table#tabid > tbody > tr:first-of-type {
background-color: yellow;
}
<table id="tabid">
<tbody>
<tr>
<td>Select me</td>
<td>cell-112</td>
<td>cell-113</td>
</tr>
<tr>
<td>cell-121</td>
<td>cell-122</td>
<td>cell-123</td>
</tr>
<tr>
<td>cell-131</td>
<td>Cell-132</td>
<td>Cell-133</td>
</tr>
<tr>
<td>
<table>
<tbody>
<tr>
<td>, but not me</td>
<td>cell-141-1</td>
<td>cell-141-1</td>
<td>cell-141-1</td>
</tr>
</tbody>
</table>
</td>
<td>cell-142</td>
<td>cell-143</td>
</tr>
</tbody>
</table>
我是一名优秀的程序员,十分优秀!