【发布时间】:2017-02-21 21:39:06
【问题描述】:
我有以下 HTML,它使用 jquery 从 csv 文件中提取数据并将其显示在表格中。我正在尝试对结果表进行一些条件格式化。我希望将值低于 2.5 的任何单元格设置为绿色背景。 2.5 到 3 之间的值应该是黄色的。 3 到 100 之间的任何值都应该是红色的。我尝试了网站上提供的许多不同的解决方案,但到目前为止都没有奏效。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatiable" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Inspection</title>
<link rel="stylesheet" href="mystyles.css" />
<meta http-equiv="refresh" content="120">
</head>
<body>
<script src="jquery.min.js"></script>
<script>
function opsi(data){
var allRows = data.split(/\r?\n|\r/);
var table = "<table id='scrapT'>";
for(var singleRow=0;singleRow<allRows.length;singleRow++){
if(singleRow === 0){
table += "<thead>";
table += "<tr>";
} else{
table += "<tr>";
}
var rowCells = allRows[singleRow].split(',');
for(var rowSingleCell=0;rowSingleCell<rowCells.length;rowSingleCell++){
if(singleRow === 0){
table += "<th>";
table += rowCells[rowSingleCell];
table += "</th>";
} else{
table += "<td>";
table += rowCells[rowSingleCell];
table += "</td>";
}
}
if(singleRow === 0){
table += "</tr>";
table += "</thead>";
table += "<tbody>";
} else{
table += "</tr>";
}
}
table += "</tbody";
table += "</table>";
$("body").append(table);
}
$.ajax({
url: "file.csv",
dataType: "text"
}).done(opsi);
</script>
【问题讨论】: