各行的 CSS 背景色

Arc*_*ger 5 html css asp.net-mvc

我在网上找到了很多关于为备用表格行着色的文章。如果我想为各行使用不同的颜色,我该怎么做?

在此处输入图片说明

<table class="table1">          
<tr>
   <th>Name</th>
   <th>Surname</th>
   <th>Email</th>
</tr>                
 @{ foreach (var p in Model.People)
     {   <tr>
             <td>@p.Name</td>
             <td>@p.Surname</td>
             <td>@p.Number</d>
         </tr>
     } 
  }
</table>
Run Code Online (Sandbox Code Playgroud)

Eri*_*tis 4

你可以在你的CSS中有

\n\n
.table1 tr:nth-child(1) {    background:blue;           }\n.table1 tr:nth-child(2) {    background:red;            }\n.table1 tr:nth-child(3) {    background:orange;         }\n...\n\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n\n

请参阅演示\n http://jsfiddle.net/wnCgL/

\n\n

编辑

\n\n

使用 jQuery,使用随机颜色函数

\n\n
$(function() {\n     $(\'.table1\').find(\'tr\').each(\n        function() {\n          $(this).css(\'background\', get_random_color());  \n        });\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

http://jsfiddle.net/wnCgL/1/

\n