【问题标题】:Changing color of whole row in a table when a condition is met?满足条件时更改表格中整行的颜色?
【发布时间】:2016-06-21 16:32:24
【问题描述】:

如何在满足条件时更改表格中整行的颜色?

例如返回FAILURE?将整行设为红色。

<tr> <!-- Disk Space Available -->
                <td class="tg-yw4l">ld</td>
                <td class="tg-yw4l">
                <?php 
                    if ($ld_status == 0) {
                        echo 'SUCCESS';
                    } else if ($ld_status == 1) {
                        echo 'WARNING';
                    } else {
                        echo 'FAILURE';
                    }   
                ?>
                </td>

【问题讨论】:

  • 使用&lt;tr&gt;标签的bgcolor属性。
  • @DanBracuk Even W3Schools(不完全以领先于曲线而闻名)注意不要使用bgcolor 属性。

标签: javascript php html css sql


【解决方案1】:

你可以用 PHP 生成 tr 的类,然后在 css 中你可以改变每个类的样式。

CSS

.success{
  background-color: green;
  }

.warning{
  background-color: yellow;
  }

.failure{
  background-color: red;
  }

HTML

<tr class=" <?php 
                    if ($ld_status == 0) {
                        echo 'success';
                    } else if ($ld_status == 1) {
                        echo 'warning';
                    } else {
                        echo 'failure';
                    }   
                ?>"> 

                <td class="tg-yw4l">ld</td>
                <td class="tg-yw4l">
                <?php 
                    if ($ld_status == 0) {
                        echo 'SUCCESS';
                    } else if ($ld_status == 1) {
                        echo 'WARNING';
                    } else {
                        echo 'FAILURE';
                    }   
                ?>
                </td>

【讨论】:

    【解决方案2】:

    试试这个,它会适合你的情况

     <html>
        <body>
        <?php $ld_status=2; ?>
        <table border="1">
        <tr style="background:<?php if($ld_status == 0) { echo 'green'; } else if ($ld_status == 1){ echo 'yellow';  } else { echo 'red';}?>;" > <!-- Disk Space Available -->
                        <td class="tg-yw4l">ld</td>
                        <td class="tg-yw4l">
                        <?php 
                            if ($ld_status == 0) {
                                echo 'SUCCESS';
                            } else if ($ld_status == 1) {
                                echo 'WARNING';
                            } else {
                                echo 'FAILURE';
                            }   
                        ?>
                        </td>
                </table>        
        </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 2021-12-12
      • 1970-01-01
      相关资源
      最近更新 更多