交替颜色 HTML Table PHP

Alternating Color HTML Table PHP

我正在尝试让我的 table 更换颜色,但我遇到了一些困难。

 if ($i % 2 == 0)
    $color = "grey";
    else
    $color = "white"; $i++;
    $table .= "<tr style=backround-color=$color>"; 

这不起作用。我也试过了,但也没用。

$table .= "<tr:nth-child(even) {background: #CCC};  tr:nth-child(odd) {background: #FFF}; >"; 

您拼写错误 background 并且您没有在 CSS 中使用 =,您使用 :。我还在您的属性值周围添加了引号,因为这是最佳做法:

$table .= "<tr style='background-color:$color'>";

你问题的最后一行甚至不接近有效 HTML 或 CSS。不过看起来还挺整洁的。

我发现了一个有趣的 link here that does what you are looking for. I put the code from the link into a jsfiddle,这是我从 link:

获得的 css 样式
.TFtableCol{
    width:100%; 
    border-collapse:collapse; 
}
.TFtableCol td{ 
    padding:7px; border:#4e95f4 1px solid;
}
/* improve visual readability for IE8 and below */
.TFtableCol tr{
    background: #b8d1f3;
}
/*  Define the background color for all the ODD table columns  */
.TFtableCol tr td:nth-child(odd){ 
    background: #b8d1f3;
}
/*  Define the background color for all the EVEN table columns  */
.TFtableCol tr td:nth-child(even){
    background: #dae5f4;
}