如何更改表中显示的值的颜色,并用不同的颜色显示(例如,如果urgent
然后将文本颜色显示为红色)?它是来自后端的价值。
Java脚本
var priority = objs[i].priority;
switch(objs[i].priority)
{
case '1': priority = "Urgent"; break;
case '2': priority = "Major"; break;
}
tr.find(".td_priority").text(priority);
最佳答案
要为文本添加颜色,请使用.css('color','red')。
它会像。
$(document).ready(function(){
objs = 1;
var priority = objs;
var color ='';
switch(objs) //in your case use objs[i].priority. I used objs for example
{
case 1: priority = "Urgent"; color = 'red'; break; //if it's Urgent, add color red
case 2: priority = "Major"; color = 'green'; break; //for example if it's Major, add color green
}
$('tr').find(".td_priority").text(priority).css('color', color); //for text add css
});
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<table>
<tr>
<td class="td_priority"></td>
</tr>
</table>
关于javascript - 根据值切换案例说明更改文本颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57068062/