[英]how to change the colour of td dynamically
我想动态更改td的颜色,这是我的代码:
<td>
<?php
if($status === 'On time'){
$color = "green";
}elseif ($status === 'No data') {
$status = "Address Not Found";
$color = "red";
}else{
$status = "Free Slot";
$color = "grey";
}
?>
<?php echo $status; ?>
<?php
}
?>
</td>
我想更改td的颜色取决于我从PHP端收到的状态。
那么如何为td动态添加颜色呢?
您应该在输出之前将color设置为变量,然后在“ td”标签内将其打印为bgcolor或css(style =“ background-color:#...”)属性。 简单的解决方案:
<?php
$color = "#000000"; //set color before output
?>
...
<td bgcolor="<?=$color?>"></td> <!--short php variable output inside html code -->
结果:
<td bgcolor="#000000"></td>
尝试这个
<?php
$status = 'On time';
if ($status === 'On time'){
$color = "green";
} elseif ($status === 'No data') {
$status = "Address Not Found";
$color = "red";
} else {
$status = "Free Slot";
$color = "grey";
}
?>
<table>
<tr>
<td style="background-color: <?php echo $color; ?>"><?php echo $status; ?></td>
</tr>
</table>
您可以使用CSS在没有任何PHP条件的情况下管理此问题的另一种方式。
<table>
<tr>
<td class="status_<?php echo $status; ?>"><?php echo $status; ?></td>
</tr>
</table>
<style>
td.status_On.time{
color: green;
}
td.status_No.data{
color: red;
}
td.status_Free.Slot{
color: grey;
}
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.