gpt4 book ai didi

html - 从 N 开始的颜色表列 (td :nth-of-type)

转载 作者:搜寻专家 更新时间:2023-10-31 22:21:47 24 4
gpt4 key购买 nike

我想从某列开始添加某种背景颜色。不必为每一列使用 css 类,如果可能的话,我宁愿使用其他方式。

我将有一个包含数百行和大约 25 列的巨大表格,我希望避免不必要的代码。

目前,我正在使用 td:nth-of-type 属性来做到这一点:

.demo tr.selectedRow td:nth-of-type(9),
.demo tr.selectedRow td:nth-of-type(10),
.demo tr.selectedRow td:nth-of-type(11),
.demo tr.selectedRow td:nth-of-type(12),
.demo tr.selectedRow td:nth-of-type(13),
.demo tr.selectedRow td:nth-of-type(14),
.demo tr.selectedRow td:nth-of-type(15),
.demo tr.selectedRow td:nth-of-type(16) {
background-color: #fff16b;
}
<table border="1" class="demo">
<tr>
<td>not selected</td>
</tr>
<tr class="selectedRow">
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
</table>

我想知道是否有任何方法可以进一步减少这种情况。

The documentation不多说了……

最佳答案

http://css-tricks.com/examples/nth-child-tester/这个测试仪真的可以提供帮助。看起来您想选择 9 之后的所有内容,因此请使用下面的代码

选择除前 8 个之外的所有 TD

.demo tr.selectedRow td:nth-child(n+9) {
color: red;
}
<table border="1" class="demo">
<tr>
<td>not selected</td>
</tr>
<tr class="selectedRow">
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>not selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
<td>selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
<tr>
<td>not selected</td>
</tr>
</table>

关于html - 从 N 开始的颜色表列 (td :nth-of-type),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14476428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com