简体   繁体   English

HTML表格交替行THBody用法

[英]HTML Table Alternating Row THBody Usage

I have several html tables in my content area of my page. 我的页面内容区域中有几个html表。 The style is weird because it doesn't start the alternating row color fresh at the start of each table, it carries it on through out the list of tables. 这种风格很奇怪,因为它不会在每个表的开头处开始新鲜的交替行颜色,它会在表格列表中进行传递。

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

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

The colour in the rows is a representation of what the css would set as the row background. 行中的颜色表示css将设置为行背景的内容。 But I want css to start the alternating again for the next table. 但我希望css再次为下一张桌子开始交替。 So it would be: 所以它会是:

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

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

Does THBODY have anything to do with it? THBODY与它有什么关系吗?

Thanks, 谢谢,

CSS Code CSS代码

table { border-collapse:collapse; text-align:center; }

table th, td { border:1px solid #759EC7; padding:3px 7px 2px; }

th { color: #fff;
background-color: #5c87b2; text-align:center; }

tr:nth-child(odd) { background-color: #CEE1F5; }
tr:nth-child(even) { background-color: #fff; }

Update 更新
It may be a bug that has crept in, I've look on the suggested fiddles and it works perfectly so it is just some buggy code somewhere. 它可能是一个悄悄进入的错误,我看看建议的小提琴,它完美地工作,所以它只是一些错误的代码。

You can easily achieve it using combinations of :nth-child() by passing even and odd values. 您可以使用以下组合轻松实现它:nth-child()通过传递evenodd数值。 For eg. 例如。 see this fiddle . 看到这个小提琴

where, the CSS is CSS在哪里

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

The only problem you have is missing the tag in the table. 你遇到的唯一问题是缺少表格中的标签。 It works perfectly if you add it. 如果添加它,它可以完美地工作。 It shouldnt have anything to do with the tbody tag. 它不应该与tbody标签有任何关系。

<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>

here is the fiddle: http://jsfiddle.net/rBwBm/ 这里是小提琴:http://jsfiddle.net/rBwBm/

I think you're doing it using javascript, right ? 我想你是用javascript做的,对吧? Probably getting a collection of tr through jquery with $('tr') ? 可能用$('tr')通过jquery获取tr的集合? Try using CSS nth-child(odd) and nth-child(even) instead, most modern browsers won't have any problem with that. 尝试使用CSS nth-child(odd)和nth-child(even)代替,大多数现代浏览器都不会有任何问题。

The issue I was having was with two <TH> rows, which through off the alternating row colouring. 我遇到的问题是两个<TH>行,它们通过交替的行着色。 So for example: 例如:

<tr>
    <th colpsan="2">Name</th>
</tr>
<tr>
    <th>First</th>
    <th>Last</th>
</tr>

This would have the Blue start on the Name row and then start alternating. 这将在Name行上启动Blue ,然后开始交替。 So the first line of the table body would be Blue 所以表体的第一行是Blue

<tr>
    <th>Name</th>
</tr>

This would have the Blue start on the Name row like before and then start alternating, However , the first line of the table body would be White 这将像以前一样在Name行上启动Blue ,然后开始交替, 但是 ,表体的第一行将是White

In these situations it would show a changing style which is not what I wanted to achieve. 在这些情况下,它会显示出一种不同于我想要实现的风格。 So all I did to fix this is: 所以我所做的就是解决这个问题:

<thead>
    <tr>
        <th colpsan="2">Name</th>
    </tr>
    <tr>
        <th>First</th>
        <th>Last</th>
    </tr>
</thead>
<tbody>
   <!-- Table Content in Here -->
</tbody>

And I then changed the style sheet to be: 然后我将样式表更改为:

tbody tr:nth-child(odd) {}
tbody tr:nth-child(even) {}

So basically I used the TBody and THead tags to make a more specific css style which is brilliant. 所以基本上我使用TBodyTHead标签来制作更具体的css风格,这很棒。 More control, flexibility. 更多控制,灵活性。 So in my new example, you can have as many rows in the THead as you like, the content should always start on White , and to answer my question: 所以在我的新例子中,你可以在THead拥有尽可能多的行,内容应始终以White开头,并回答我的问题:

Does THead have anything to do with it? THead与它有什么关系吗?

Yes, it has EVERYTHING to do with it. 是的,它有一切与它有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM