有 3 种不同的颜色,颜色按顺序重复到表格末尾

<table border="1" cellpadding="10" cellspacing="10" style="border-collapse:collapse; width:200px; margin:auto">

    <?php

        $colors = array("#376ee5", "#3c763d", "#e2b70b");

        for($j = 1; $j < 11; $j++){ 

            for($i = 0; $i < count($colors); $i++){

                echo "<tr style='background-color:$colors[$i]'><td>$j</td><td>Student $j</td></tr>";

            }

        }

    ?>

</table>

如图所示如何创建?有没有办法像图片中一样创建列颜色?请帮我

http://img4.mukewang.com/6364cd7c00011c3b03020803.jpg

慕田峪4524236
浏览 115回答 2
2回答

胡说叔叔

使用 CSS:td:nth-child(3n){&nbsp; &nbsp; background-color: #376ee5}td:nth-child(3n+1){&nbsp; &nbsp; background-color: #3c763d}td:nth-child(3n+2){&nbsp; &nbsp; background-color: #e2b70b}

largeQ

我会通过为$colors将循环通过 0-2 的数组设置一个索引来做到这一点:$colors = array("#376ee5", "#3c763d", "#e2b70b");$colorIndex = 0;for($j = 1; $j < 11; $j++){&nbsp; &nbsp; echo "<tr style='background-color:$colors[$colorIndex]'><td>$j</td><td>Student $j</td></tr>";&nbsp; &nbsp; $colorIndex++;&nbsp; &nbsp; $colorIndex = $colorIndex > 2 ? 0 : $colorIndex;}
打开App,查看更多内容
随时随地看视频慕课网APP