duanfen2349 2014-09-27 04:50
浏览 118

如何相互改变<li>或<tr>颜色

i want to change the color of li or tr whatever possible after one another.

Note:- I want to call this in while loop so static class will be no solution

I want the output lke:-

<li>line1</li> //white background
<li>line2</li> //red background
<li>line3</li> //white background
<li>line4</li> //red background

or if possible in like:-

<tr>
<td>line1</td>  //white background
</tr>
<tr>
<td>line2</td>  //red background
</tr>
<tr>
<td>line3</td>  //white background
</tr>
<tr>
<td>line4</td>  //red background
</tr>

What i am trying yet is:-

<?php
$fields = CFS()->get('gallery');
foreach ($fields as $field) { <?
<table>
<tr><td><?php echo $field['slide_title']; ?></td></tr>
<tr><td><?php echo $field['upload']; ?></td></tr>
</table>
<?php }  ?>
  • 写回答

4条回答 默认 最新

  • douqin231881 2014-09-27 04:53
    关注

    You could achieve this by CSS :nth-of-type odd and even selector.

    JSFiddle - DEMO

    li:nth-of-type(odd) {
        color:red;
    }
    li:nth-of-type(even) {
        color:blue;
    }
    

    And for tds selector:

    JSFiddle - DEMO

    table tr:nth-of-type(odd) {
        color:red;
    }
    table tr:nth-of-type(even) {
        color:blue;
    }
    

    You could also use CSS :nth-child selector like this:

    JSFiddle - DEMO

    table tr:nth-child(2n+1) {
        color:red;
    }
    table tr:nth-child(2n) {
        color:blue;
    }
    

    NOTE: tr:nth-child(2n) - Represents the even rows of an HTML table.

    and tr:nth-child(2n+1) - Represents the odd rows of an HTML table.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端