首页 文章

表行背景颜色的PHP速记变量切换

提问于
浏览
0

我最近在php中使用以下简写来定义表格行背景颜色 .

Are there are caveats to using this method ,除了默默无闻之外?

<?php $style = 'style="background-color:#CCC;"'; ?>
 <tr <?php if ($i = !$i) echo $style; ?>>
      <td><input /></td>
 <tr>

发生的事情是 $i = !$i 意味着$ i不能等于$ i,所以如果第一次$ i是真的,它就变成了假,反之亦然 . if 当然每次都检查值 true or false ,从而每隔一段时间输出一次样式并获得每隔一个背景效果 .

1 回答

  • 0

    我没有得到你的$ i!= $ i的逻辑,但是如果我理解你想要交替TR的不同颜色,它可以在css级别完成 . 使用CSS - 第n个属性 .

    尝试:

    tr:nth-child(odd) {
        background: red;
    }
    
    tr:nth-child(even) {
        background: blue;
    }
    

    要么

    tr:nth-child(2n+1) {
         background: yellow;
    }
    

    注意: Use 2n+02n+1 表示备用 - 从第一行2开始,或第二行2

相关问题