更新了HTML:
我使用一个插件使我的HTML表可折叠,该插件为没有其他任何父级的行提供了data-parent=""。在这里,我仅在以下HTML中显示父行,而不显示其子级:

<table>
<thead>
    <tr>
        <th>Name</th>
        <th>Week1</th>
        <th>Week2</th>
        <th>Week3</th>
        <th>Week4</th>
    </tr>
</thead>
<tbody>
    <tr data-parent=""> //should be grey
        <td>+John</td>
        <td>10</td>
        <td>50</td>
        <td>20</td>
        <td>30</td>
    </tr>
    <tr>
        <td>Hunohn</td>//ignore
        <td>10</td>
        <td>50</td>
        <td>20</td>
        <td>30</td>
    </tr>
    <tr data-parent="">//white
        <td>+Boney</td>
        <td>90</td>
        <td>40</td>
        <td>10</td>
        <td>80</td>
    </tr>
    <tr data-parent=""> //grey
        <td>Dwihn</td>
        <td>10</td>
        <td>50</td>
        <td>20</td>
        <td>30</td>
    </tr>
    <tr data-parent="">//white
        <td>+Arkon</td>
        <td>80</td>
        <td>20</td>
        <td>70</td>
        <td>50</td>
    </tr>
    <tr>
        <td>Tyulor</td>//ignore
        <td>10</td>
        <td>50</td>
        <td>20</td>
        <td>30</td>
    </tr>
</tbody>
</table>


我想仅对具有data-parent=""的行赋予备用行颜色,而与它具有的数据无关。数据行是可排序的。即使使用data-parent=""对行进行排序后,它们也应具有备用的行颜色。

最佳答案

这可替代地针对“数据父级”。代码已更新



table tr {
  background: #fff;
}
tbody tr[data-parent=""]{
  background: grey;
}
tbody tr[data-parent=""] ~ tr[data-parent=""]:nth-child(even){
  background: yellow;
}

<table>
<thead>
    <tr>
        <th>Name</th>
        <th>Week1</th>
        <th>Week2</th>
        <th>Week3</th>
        <th>Week4</th>
    </tr>
</thead>
<tbody>
    <tr data-parent=""> //should be grey
        <td>+John(grey)</td>
        <td>10</td>
        <td>50</td>
        <td>20</td>
        <td>30</td>
    </tr>
    <tr>
        <td>Hunohn</td>//ignore i.e white as background color
        <td>10</td>
        <td>50</td>
        <td>20</td>
        <td>30</td>
    </tr>
    <tr>
        <td>Hunohn</td>//ignore i.e white as background color
        <td>10</td>
        <td>50</td>
        <td>20</td>
        <td>30</td>
    </tr>
    <tr data-parent="">//yellow
        <td>+Boney(yellow)</td>
        <td>90</td>
        <td>40</td>
        <td>10</td>
        <td>80</td>
    </tr>
    <tr data-parent=""> //grey
        <td>+Dwihn(grey)</td>
        <td>10</td>
        <td>50</td>
        <td>20</td>
        <td>30</td>
    </tr>
    <tr data-parent="">//yellow
        <td>+Arkon(yellow)</td>
        <td>80</td>
        <td>20</td>
        <td>70</td>
        <td>50</td>
    </tr>
    <tr data-parent="6">
        <td>Tyulor</td>//ignore
        <td>10</td>
        <td>50</td>
        <td>20</td>
        <td>30</td>
    </tr>
</tbody>
</table>

10-06 02:52