简体   繁体   English

如何仅在使用PHP动态生成的tr的最后一行中设置背景色

[英]How to set background-color in only last row of tr which is dynamically generated using php

<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>

You can use CSS :last-child pseudo to achieve that.. 您可以使用CSS :last-child伪来实现这一点。

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

Demo 演示

...in css: ...在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>

If you would need to do it in your php loop, you can reverse your counter: 如果您需要在php循环中执行此操作,则可以反转计数器:

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

try this 尝试这个

<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>

OR you can Add a CSS Like this as @Mr. 或者,您可以像这样将CSS添加为@Mr。 Alien answered 外星人回答

#your_table_id tr:last-child {
   background: orange;
}

Use :last-child 使用:last-child

CSS CSS

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

Or 要么

jQuery jQuery的

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

I gave jQuery option too because :last-child CSS selector has compatibility issue 我也给了jQuery选项,因为:last-child CSS选择器存在兼容性问题

Hope this is what you want 希望这就是你想要的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM