[英]Conditional Formatting Values in Table
我正在尝试对表中的值设置一些条件格式。 例如,我有2列,1行,因此有2个值(每个td中有1个)。 我正在尝试将表格中包含文本“ Red”的行中任何值的字体颜色设置为红色。
我似乎没有收到错误。
解决:最终代码:
<?!= 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>
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>
您需要使用document.getElementById("Red").setAttribute("style","color:red");
将表格的tds的字体颜色设置为红色?
另外,正如@skirator所问的那样,您能告诉我们您的桌子吗?
我不完全理解通过将每个数据传递给类似的函数来完成的工作,但是如果情况是您有一个表,并且表中的每个单元格可能包含单词“ 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')
}
创建表后尝试此脚本
<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.