简体   繁体   English

使用CSS样式表

[英]Styling tables using CSS

I have a problem with styling tables using CSS. 我在使用CSS样式表时遇到问题。

So I have a table in my HTML file: 所以我的HTML文件中有一个表:

<table class="altrowstable" id="alternatecolor">
<tr>
    <th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th>
</tr>
<tr>
    <td>Text 1A</td><td>Text 1B</td><td>Text 1C</td>
</tr>
<tr>
    <td>Text 2A</td><td>Text 2B</td><td>Text 2C</td>
</tr>
<tr>
    <td>Text 3A</td><td>Text 3B</td><td>Text 3C</td>
</tr>
<tr>
    <td>Text 4A</td><td>Text 4B</td><td>Text 4C</td>
</tr>
<tr>
    <td>Text 5A</td><td>Text 5B</td><td>Text 5C</td>
</tr>
</table>

Here is my JavaScript file: 这是我的JavaScript文件:

function altRows(id){
if(document.getElementsByTagName){  

    var table = document.getElementById(id);  
    var rows = table.getElementsByTagName("tr"); 

    for(i = 0; i < rows.length; i++){          
        if(i % 2 == 0){
            rows[i].className = "evenrowcolor";
        }else{
            rows[i].className = "oddrowcolor";
        }      
    }
  }
 }
 window.onload=function(){
altRows('alternatecolor');
 }

And here is my CSS file: 这是我的CSS文件:

table.altrowstable {
    font-family: verdana,arial,sans-serif;
    font-size:11px;
    color:#333333;
    border-width: 1px;
    border-color: #a9c6c9;
    border-collapse: collapse;
}
table.altrowstable th {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #a9c6c9;
}
table.altrowstable td {
    border-width: 1px;
    padding: 8px;
        border-style: solid;
    border-color: #a9c6c9;
}

table.oddrowcolor{
    background-color:#d4e3e5;
}
table.evenrowcolor{
    background-color:#c3dde0;
}

The problem is that it is not changing color neither odd rows nor even odd. 问题在于它既不会改变颜色,也不会改变奇数行甚至奇数。

What am I doing wrong? 我究竟做错了什么?

Thanks. 谢谢。

I'll provide you a CSS solution for this: 我将为此提供一个CSS解决方案:

table.class_name tr:nth-child(odd) {
   /* Styles */
}

table.class_name tr:nth-child(even) {
   /* Styles */
}

That's all you need, although it's not supported in IE 8 and earlier. 这就是您所需要的,尽管IE 8和更早版本不支持它。

Demo 演示版

For your table headers, you can simply use a different selector to over ride the background styles like 对于表格标题,您可以简单地使用其他选择器来覆盖背景样式,例如

table.altrowstable tr th {
    background: #fff;
}

Demo 2 演示2

I did check your code and found a little correction iin the css is needed to get the expected solution. 我确实检查了您的代码,发现需要对CSS进行一些更正以获得所需的解决方案。 There should be an empty space between the table and row classname. 表和行的类名之间应该有一个空格。

table .oddrowcolor{
   background-color:#d4e3e5;
}
table .evenrowcolor{
   background-color:#c3dde0;
}

I like to provide solutions that dont tinker or modify the original source much. 我喜欢提供一些不会修改修改原始资源的解决方案。 Your HTML is fine, JScript is fine(very fine). 您的HTML很好, JScript很好(很好)。 Good to see that you use the .classname so that is is cross brwoser compatible.So all i did is change the classes for the CSS 很高兴看到您使用.classname以便跨浏览器兼容。所以我所做的就是更改CSS的类。

YOUR CODE 您的密码

table.oddrowcolor {
    background-color:#d4e3e5;
}
table.evenrowcolor {
    background-color:#c3dde0;
}

MY CHANGE 我的改变

tr.oddrowcolor {
    background-color:#d4e3e5;
}
tr.evenrowcolor {
    background-color:#c3dde0;
}

WORKING FIDDLE 工作场所

total change from your code to mine. 从您的代码到我的代码的总更改。 8 characters. 8个字符。 Simple ain't it? 简单不是吗?

You have a problem within your CSS. 您的CSS内有问题。 You are setting the class for table, while as it should be for td. 您正在为表设置类,而应为td设置类。

You also need to modify your below js as style can be applied to td and not to tr 您还需要修改下面的js,因为样式可以应用于td而不是tr

var rows = table.getElementsByTagName("td"); 

Here is the problem in your CSS 这是您的CSS中的问题

table.oddrowcolor{
    background-color:#d4e3e5;
}
table.evenrowcolor{
    background-color:#c3dde0;
}

You should be using this instead 你应该用这个代替

table td.oddrowcolor{
    background-color:#d4e3e5;
}
table td.evenrowcolor{
    background-color:#c3dde0;
}

jsFiddle jsFiddle

Try this one ... see the Demo 试试这个... 看演示

tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}

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

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