Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML表格交替行体用法_Html_Css - Fatal编程技术网

HTML表格交替行体用法

HTML表格交替行体用法,html,css,Html,Css,在我的页面的内容区域中有几个html表。这种样式很奇怪,因为它不会在每个表的开始处使用新的交替行颜色,而是在整个表列表中使用 <table> <tr> Blue </tr> <tr> White </tr> <tr> Blue </tr> </table> <table> <tr>

在我的页面的内容区域中有几个html表。这种样式很奇怪,因为它不会在每个表的开始处使用新的交替行颜色,而是在整个表列表中使用

<table>
    <tr>
       Blue
    </tr>
    <tr>
       White
    </tr>
    <tr>
       Blue
    </tr>
</table>

<table>
    <tr>
        White
    </tr>
    <tr>
        Blue
    </tr>
    <tr>
        White
    </tr>
</table>
更新

这可能是一个已经潜入的bug,我已经查看了建议的小提琴,它工作得非常完美,因此它只是某个地方的一些bug代码。

通过传递
偶数和
奇数,您可以使用
:nth-child()
的组合轻松实现它。例如,见

其中,CSS是

body {
    background-color: black;
    color: red;
}
table tr:nth-child(odd) {
    background-color: blue;
}
table tr:nth-child(even) {
    background-color: #ffffff;
}

唯一的问题是缺少表中的标记。 如果你加上它,它会工作得很好。它不应该和车身标签有任何关系

<table>
    <tr>
        <td>Blue</td>
    </tr>
    <tr>
        <td>White</td>
    </tr>
    <tr>
        <td>Blue</td>
    </tr>
</table>
<table>
    <tr>
        <td>Blue</td>
    </tr>
    <tr>
        <td>White</td>
    </tr>
    <tr>
        <td>Blue</td>
    </tr>
</table>

蓝色
白色
蓝色
蓝色
白色
蓝色

我想你是用javascript来做的,对吗?可能是通过jquery和$('tr')获得tr的集合?尝试使用CSS第n个孩子(奇数)和第n个孩子(偶数),大多数现代浏览器不会有任何问题。

我遇到的问题是有两行
通过交替的行着色。例如:

<tr>
    <th colpsan="2">Name</th>
</tr>
<tr>
    <th>First</th>
    <th>Last</th>
</tr>
这将使
蓝色
像以前一样在
名称
行上开始,然后交替开始,但是,表体的第一行将是
白色

在这些情况下,它会显示出一种变化的风格,这不是我想要实现的。所以我所做的一切就是:

<thead>
    <tr>
        <th colpsan="2">Name</th>
    </tr>
    <tr>
        <th>First</th>
        <th>Last</th>
    </tr>
</thead>
<tbody>
   <!-- Table Content in Here -->
</tbody>
因此,基本上我使用了
TBody
THead
标记来创建一个更具体的css样式,它非常出色。更多的控制、灵活性。因此,在我的新示例中,您可以在
THead
中有任意多行,内容应始终以
白色开始,并回答我的问题:

THead
与此有关吗


是的,它有与之相关的一切。

你也可以发布你的CSS吗?我想我可能在某个地方发现了一个bug。它确实显示它工作得很好。我没有错过标签,它是一个颜色的文本表示,应该在该rowwell中描绘,表示与否,这个标记应该工作。。。我把它放在JSFIDLE中,它与css一起工作。很抱歉,从我的角度来看,没有TD的TR是无效的HTML,所以在您的示例中,您丢失了TD标记……这就是重点。我知道它不是有效的标记,如果你读过的话,这不是我问题的重点。我对HTML很有经验,我不会忘记把
TD
标记放进去。这只是一个文本表示的颜色,这一行应该是。不,看看我的问题,它是在css中完成的,无论如何,我认为这可能是一个错误
<tr>
    <th colpsan="2">Name</th>
</tr>
<tr>
    <th>First</th>
    <th>Last</th>
</tr>
<tr>
    <th>Name</th>
</tr>
<thead>
    <tr>
        <th colpsan="2">Name</th>
    </tr>
    <tr>
        <th>First</th>
        <th>Last</th>
    </tr>
</thead>
<tbody>
   <!-- Table Content in Here -->
</tbody>
tbody tr:nth-child(odd) {}
tbody tr:nth-child(even) {}