我正在尝试为$row->cap24hrChange
结果执行条件格式设置。如果是< 0
,我希望该值为红色,否则应为绿色。我没有收到剧本的任何回复。
<table><head><style>
td {text-align: right;}
</style></head><table>
<thead>
<tr>
<th>#Rank</th>
<th>Name</th>
<th>Price</th>
<th>Mkt Cap</th>
<th>Volume</th>
<th>Supply</th>
<th>24h(%)</th>
</tr>
</thead>
<tbody>
<?php function compare($a, $b) {
return intval($a->position24) - intval($b->position24);
}
$json = file_get_contents('http://www.coincap.io/front');
$data = json_decode($json);
usort($data, 'compare');
?>
<?php foreach ($data as $row) { ?>
<tr>
<td><?= $row->position24; ?></td>
<td><?= $row->long; ?></td>
<td><?= number_format($row->price, 4); ?><\td>
<td><?= number_format($row->mktcap, 2); ?><\td>
<td><?= number_format($row->volume, 2); ?><\td>
<td><?= number_format($row->supply, 2); ?><\td>
<td><?= $row->cap24hrChange; echo "<script type=\"text/javascript\"> var trTags = document.getElementsByTagName("td"); for (var i = 0; i < trTags.length; i++) { var tdEightEl = trTags[i].children[7]; if (tdEightEl.innerText < 0) {tdEightEl.style.color = "red"; } else if (tdEightEl.innerText > 0) {tdEightEl.style.color = "green"; } }</script>";?></td>
</tr>
<?php } ?>
</tbody>
</table>
修改
在一个非常相关的说明中:我如何处理该代码,以便在右侧包含图标(↑和↓),分别为&gt; 0和&lt; 0?
修改(2)
我在$foo = "bar".$foo
<td style="color:<?php if($row->cap24hrChange > 0){ echo "green"; $row->cap24hrChange = $row->cap24hrChange . " ";}else if($row->cap24hrChange < 0){ echo "red"; $row->cap24hrChange = $row->cap24hrChange . " ";}?>"><?= $row->cap24hrChange;?></td>
答案 0 :(得分:1)
不要使用javascript来做到这一点。这是你在页面加载前可以知道的东西,所以你可以把它放在静态。
<td style="color:<?php if($row->cap24hrChange > 0){ echo "green"; }else if($row->cap24hrChange < 0){ echo "red"; }?>"><?= $row->cap24hrChange;?></td>
但请注意,您的代码中没有$ row-&gt; cap24hrChange == 0的情况。