【发布时间】:2021-02-16 16:08:54
【问题描述】:
我有以下 HTML:
<table id="price-table">
<tr><th>Price</th><th>Value</th></tr>
<tr class="price-row">
<td>0</td>
<td>1</td>
</tr>
<tr><th>Price</th><th>Value</th></tr>
<tr class="price-row">
<td>1</td>
<td>2</td>
</tr>
</table>
我正在尝试使用以下方法为每个奇数行设置价格行的样式:
#price-table tr.price-row:nth-child(odd) {
color: green;
}
每个偶数行使用:
#price-table tr.price-row:nth-child(even) {
color: blue;
}
但是,只有偶数行被设置样式,奇数行被完全忽略。我不明白这是为什么?第一行的 id 不是 1 会被设置为奇数,第二行的 id 不是 2 会被设置为偶数吗?
我应该提一下,我确实希望此表可以扩展,以便能够添加更多行并正确设置样式。
非常困惑,我不知道还能尝试什么,这似乎并不复杂,除非我犯了一个非常愚蠢的错误。
编辑完整的 HTML:
<table id="price-table">
<tr>
<th>Price</th>
<th>Value</th>
</tr>
<tr class="price-row">
<td>0</td>
<td>1</td>
</tr>
<tr>
<th>Price</th>
<th>Value</th>
</tr>
<tr class="price-row">
<td>1</td>
<td>2</td>
</tr>
<tr>
<th>Price</th>
<th>Value</th>
</tr>
<tr class="price-row">
<td>0</td>
<td>1</td>
</tr>
<tr>
<th>Price</th>
<th>Value</th>
</tr>
<tr class="price-row">
<td>1</td>
<td>2</td>
</tr>
</table>
【问题讨论】:
标签: html css css-selectors pseudo-class