php - 根据数据库中的值制作表格行背景颜色
问题描述
所以我的表是这样的:
ID | photo | ident | status
------------------------------------------------
80 | img/photo1 | ACH3882 | V
81 | img/photo2 | SHD8837 | V
82 | img/photo3 | SFF4837 | X
83 | img/photo4 | DLL3266 | V
现在,只有status
列被着色,但我希望整行都有颜色,基于单元格中的值。但我不知道该怎么做。
这是我的桌子,也是我目前使用的:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="blueTable" border="2" style= "background-color: #f9f9f9; color: #000000; margin: 0 auto;" >
<thead style= "background-color: #FFFFFF">
<tr>
<th>Photo</th>
<th>Ident</th>
<th>Status</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
if ($result = $link->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
echo
"<tr>
<td>{$row['photo']}</td>
<td>{$row['ident']}</td>
<td class='statusclass'>{$row['status']}</td>
<td><a href='delete.php?id={$row['id']};'>Delete</a></td>
</tr>";
}
/*freeresultset*/
$result->free();
}
?>
</tbody>
</table>
这是添加颜色的脚本:
<script>
$( document ).ready(function() {
$(".statusclass").each(function(){
if($(this).text() == "V"){
$(this).css("background-color", "#FFEE58");
}else if($(this).text() == "X"){
$(this).css("background-color", "#66BB6A");
}
})
});
</script>
解决方案
所以你说你希望父元素被着色,所以你可以使用它。只需插入.parent()
行中。
<script>
$( document ).ready(function() {
$(".statusclass").each(function(){
if($(this).text() == "V"){
$(this).parent().css("background-color", "#FFEE58");
}else if($(this).text() == "X"){
$(this).parent().css("background-color", "#66BB6A");
}
})
});
</script>
推荐阅读
- python - 使 Docker 容器使用安装的最新版本的 Python
- html - 无法理解深层链接和意图
- r - 如果在闪亮的代码中发生某些事情,则创建一条消息
- android - SDK 中的 Android 后台服务 > 26 与 SignalR
- maven - maven-surefire-plugin 在 gitlab-ci 上使用 OpenJDK 17 失败
- angular - 带有 Nginx 的 AWS 负载均衡器背后的 Angular:拒绝的 MIME 类型
- snowflake-cloud-data-platform - 一周第一天的值
- node.js - 持久化通过渲染发送到 Pug 模板的数据
- r - 从 sum() 和 '+' 获得不同的结果
- iphone - Xcode 13 中的 Interface Builder 缺少 iPhone 模拟器