如何在tr的最后一行设置背景颜色,这是使用php动态生成的

时间:2014-02-22 12:24:04

标签: php html css

<table width="100%">
    <tr>
        <?
        $count=0;
        $i=1;
        foreach($gallery as $key => $list1)
        {
            $count++;
            ?>
            <td>
                <img src='<?echo base_url();?>userfiles/eventsgallery/small/<?=$list1['image'];?>'>
            </td>
            <? // style='background:red;'
            if($count % 4 == 0 )
            {
                $color = ""; 
                $i++;
                if($i % 2 == 0)
                {
                    $color = "style='background:orange;'";  
                }
                else
                {
                    $color = "style='background:black;'";
                }
                echo "</tr><tr ".$color.">";
            }
        }
        ?>
    </tr>

5 个答案:

答案 0 :(得分:3)

您可以使用CSS :last-child伪来实现这一目标..

table.class_name tr:last-child {
   background: #f00;
}

Demo

答案 1 :(得分:0)

...在css:

 .table1 tr:last-child {
        background-color: #0B615E;
        }



<table width="100%" class="table1">

        <tr>
              <?
            $count=0;
            $i=1;
              foreach($gallery as $key => $list1)
               {
                  $count++;

                  ?>                       
                         <td>                                                   
                           <img src='<?echo base_url();?>userfiles/eventsgallery/small/<?=$list1['image'];?>'>
                         </td>          

                    <?
                        // style='background:red;'
                        if($count % 4 == 0 )
                        {
                            $color = ""; 
                            $i++;
                            if($i % 2 == 0)
                            {
                              $color = "style='background:orange;'";  
                            }
                            else
                            {
                              $color = "style='background:black;'";
                            }

                            echo "</tr><tr ".$color.">";


                        }

              }?>
          </tr>
          </tr>
          </tr>

        </table>

答案 2 :(得分:0)

如果您需要在php循环中执行此操作,则可以撤销计数器:

<?php
$count = count($gallery);
foreach($gallery as $key => $list1) {
        $lastClass = $count == 1?' lastrow':'';
        $count--;
        ?>
       html here
<?php 
}
?>

答案 3 :(得分:-1)

试试这个

<table width="100%" id="your_table_id">
    <?
        $count=0;
        $i=1;
        $size = sizeof($gallery);
        foreach($gallery as $key => $list1) {
            $count++;
            $color = ""; 
            if($size==$count) {
                $color = "style='background:orange;'";  
            }
    ?> 
    <tr <?php echo $color;?>>
        <td>                                                   
            <img src='<?echo base_url();?>userfiles/eventsgallery/small/<?=$list1['image'];?>'>
        </td>    
    </tr>       
    <?php
        }
    ?>
</table>

或者您可以像@Mr. Alien已解答

一样添加CSS
#your_table_id tr:last-child {
   background: orange;
}

答案 4 :(得分:-1)

使用:last-child

<强> CSS

table tr:last-child {
   background-color:#ccc;
}

或者

jQuery

$("table tr").last().css('background','#ccc')

我也提供了jQuery选项,因为:last-child CSS选择器存在兼容性问题

希望这是你想要的

http://jsfiddle.net/HarishBoke/Xen99/