使用php和js更改表格单元格中文本的颜色
我有一个表格,我希望在某些里程碑上改变颜色的日子。
我有JS,但它只适用于第一个单元而不是其他单元。任何想法如何循环js所以它改变表中的每个单元格DAYS?
I have a form which i want the days to change colour at certain milestones. I have the JS but it only works for the first cell and not the others. Any ideas how to loop the js so it changes every cell called DAYS in the table?
我已将ID放在表格中,但可能是错误的。
Ive put the ID after in the table but probably wrong .
$(function () {
// Score Color
var score = parseInt($('#DAYS').text().trim());
var color = 'red';
if (!isNaN(score)) {
if (score >= 40) {
color = 'orange';
}
if (score >= 60) {
color = 'green';
}
$('#DAYS').css('color', color);
}
});
PHP table:
echo"<td id=DAYS>".$applicant_card['DAYS']."</td>";
正如DrRoach所说,我们应该使用 class
而不是 id
如果我们要定位多个项目。由于 id
总是只指向一个唯一元素。
As DrRoach told,we should use class
instead of id
if we are targetting multiple items. As id
always points to only one unique element.
所以你可以给 class
as DAYS
而不是 id
并使用每个元素运行()
给予他们尊重的颜色。
So you can give class
as DAYS
instead of id
and run through each element using each()
to give them respectice color.
示例代码
$(function () {
$('.DAYS').each(function(){
var score = parseInt($(this).text().trim());
var color = 'red';
if (!isNaN(score)) {
if (score >= 40) {
color = 'orange';
}
if (score >= 60) {
color = 'green';
}
$(this).css('color', color);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
PHP table:
<table>
<tr><td class=DAYS>34</td></tr>
<tr><td class=DAYS>44</td></tr>
<tr><td class=DAYS>74</td></tr>
<tr><td class=DAYS>23</td></tr>
<tr><td class=DAYS>54</td></tr>
</table>