微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

css – 选择具有特定类的每隔一行

我有一个简单的表结构
<table class="striped">
    <tr class="parent"><td></td></tr>
    <tr class="parent"><td></td></tr>
    <tr class="parent"><td></td></tr>
    <tr class="parent"><td></td></tr>
</table>

我可以轻松添加CSS:

.striped tbody tr.parent:nth-of-type(odd) { background:red; }

麻烦的是当你点击其中一个父< tr>它将展开的行并添加一个额外的< tr>细节行如下:

<table class="striped">
    <tr class="parent"><td></td></tr>
    **<tr class="detail"><td></td></tr>**
    <tr class="parent"><td></td></tr>
    <tr class="parent"><td></td></tr>
    <tr class="parent"><td></td></tr>
</table>

是否有纯CSS方式来保持原始条带化? Here is a codepen显示两个表发生了什么

解决方法

如果您可以选择稍微更改HTML,则可以使用多个显式< tbody>元素并将子元素插入到同一个< tbody>内作为父母.这实际上具有语义意义,并​​且它将父级与其子组合在一起.

https://developer.mozilla.org/en/docs/Web/HTML/Element/tbody

multiple <tbody> elements are permitted (if consecutive),allowing the data-rows in long tables to be divided into different sections,each separately formatted as needed

例如.

.striped tbody:nth-of-type(odd) .parent { background:red; }
<table class="striped">
    <tbody>
        <tr class="parent"><td>Stuff</td></tr>
        <tr class="child"><td>child</td></tr>
    </tbody>
    <tbody><tr class="parent"><td>Stuff</td></tr></tbody>
    <tbody><tr class="parent"><td>Stuff</td></tr></tbody>
    <tbody><tr class="parent"><td>Stuff</td></tr></tbody>
</table>

原文地址:https://www.jb51.cc/css/215622.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。