[英]Conditional Formatting Values in Table
I'm trying to set some conditional formatting on values within a table. 我正在尝试对表中的值设置一些条件格式。 For example I have 2 columns, 1 Row, so 2 values (1 in each td).
例如,我有2列,1行,因此有2个值(每个td中有1个)。 I am trying to set the font colour of any values in the row that contain the text "Red" in the table to red.
我正在尝试将表格中包含文本“ Red”的行中任何值的字体颜色设置为红色。
I don't seem to be getting errors. 我似乎没有收到错误。
RESOLVED: Final Code: 解决:最终代码:
<?!= include('script_js'); ?>
<? var data = getData(); ?>
<TABLE id="X">
<tbody>
<? for (var i = 0; i < data.length; i++) { ?>
<tr>
<? for (var j = 0; j < data[i].length; j++) { ?>
<td><?= data[i][j] ?></td>
<? } ?>
</tr>
<? } ?>
<style>
table {
border-collapse: collapse;
width: 100%;
background-color: #EEEEEE;
font-family:arial
}
tr {
border: 1px solid #DDDDDD;
width: 110%;
}
</style>
</tbody>
</TABLE>
<?!= include('script_rag'); ?>
script_rag: script_rag:
<script>
var table = document.getElementById("X");
for (var i = 0, row; row = table.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
if (col.innerText == "RED") {
col.style.backgroundColor = "#f00";
}
else
if (col.innerText == "GREEN") {
col.style.backgroundColor = "#8CC10C";
}
else
if (col.innerText == "AMBER") {
col.style.backgroundColor = "#FFBF00";
}
}
}
</script>
You want with document.getElementById("Red").setAttribute("style","color:red");
您需要使用
document.getElementById("Red").setAttribute("style","color:red");
to put the font color of your tds of your table to red ? 将表格的tds的字体颜色设置为红色?
Also as @skirator asks, can you please show us your table ? 另外,正如@skirator所问的那样,您能告诉我们您的桌子吗?
I don't completely understand what you are trying to accomplish by passing each piece of data into a function like that, but if the case is that you have a table, and each cell inside the table might contain the word "red", then I would to it like this. 我不完全理解通过将每个数据传递给类似的函数来完成的工作,但是如果情况是您有一个表,并且表中的每个单元格可能包含单词“ red”,则我想这样。
var cells = document.getElementById("#id_of_your_table").getElementsByTagName("td"); //This is an array of the cells (td's)
for (var i = 0; i < cells.length; i++) {
if (cells[i].indexOf('Red')) {
cells[i].css('background-color', 'red')
}
try this script after the table creation 创建表后尝试此脚本
<script>
var table = document.getElementById("table1");
for (var i = 0, row; row = table.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
if (col.innerText == "Red") { // You might want to look at other solutions in case the TD should contain more than just "Red"
col.style.backgroundColor = "#f00";
}
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.