【发布时间】:2019-07-18 10:45:55
【问题描述】:
我正在尝试使用 JavaScript 和 HTML 创建一些条件格式。
基本上我有一个包含以下字段的表格:
table
system
timestamp
total_amount
amount_used
amount_free
我想要根据列amount_free的值来改变字段的颜色。
在这种情况下,如果我的 amount_free 列是 0 €,那么我想在我的单元格上显示红色。
为此,我使用以下代码:
$(document).ready(function() {
$('#table_id td.amount_free').each(function() {
if ($(this).text() == '0 €') {
$(this).css('background-color', '#f00');
}
});
});
table,
td {
word-wrap: keep-all;
border: 1px solid black;
border-collapse: collapse;
}
table,
th {
border: 1px solid black;
}
th,
td {
padding: 3px;
}
,
th {
text-align: left;
}
,
th {
background-color: #f2f2f2;
}
,
td {
font-family: arial;
font-size: 10pt;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table style=width:100%>
<tr>
<th>table</th>
<th>system</th>
<th>timestamp</th>
<th>total_amount</th>
<th>amount_used</th>
<th>amount_free</th>
</tr>
<tr>
<td>amount_values</td>
<td>users</td>
<td>2019-07-18 00:00:00</td>
<td>398 €</td>
<td>179 €</td>
<td>0 €</td>
</tr>
</table>
但它不会返回任何颜色。只有表格没有格式化。 谁能帮帮我?
【问题讨论】:
-
请提供格式化代码
-
选择器
'#table_id td.amount_free'没有引用任何元素,所以你的jquery脚本基本上什么都不做 -
<td>上也没有类,所以td.amount_free没有匹配项
标签: javascript jquery html conditional-formatting