根据值切换案例说明更改文本颜色

时间:2019-07-17 03:16:47

标签: javascript jquery css html-table

如何更改表中显示的值的颜色(如果为urgent,则文本颜色为红色)?它是后端的值。

JavaScript

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);

4 个答案:

答案 0 :(得分:0)

您可以使用jquery上的css方法 https://www.w3schools.com/jquery/jquery_css.asp

如果文本颜色是预定义的。您可以为这些颜色创建不同的类,并仅基于switch更新className

答案 1 :(得分:0)

要为文本添加颜色,请使用 .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>

答案 2 :(得分:0)

In Here The problem might be You check case as a string value.But I think 
objs[i].priority returns an integer value not string type.

In Your Answer
==============
var priority = objs[i].priority;
switch(objs[i].priority) 
{
//checks case as a string so value of priority does not changes (Does not 
//true the case)
  case '1': priority = "Urgent"; break; //checks case as a string so nothing changes
  case '2': priority = "Major"; break; //checks case as a string so nothing changes
}
tr.find(".td_priority").text(priority);

-------------------------------------------------------------------------

So try this,
============

var priority = objs[i].priority;
switch(priority)
{
  case 1: priority = "Urgent"; break; //Remove Quote Marks
  case 2: priority = "Major"; break; //Remove Quote Marks
}
tr.find(".td_priority").text(priority);

答案 3 :(得分:0)

尝试类似的操作

var myown = objs[i].myown; 
switch(myown) 
{ 
case 1: 
myown = "Green"; 
break; 
case 2: 
myown = "Red"; 
break; 
} 

然后

tr.find(".td_pmyown").text(myown);

希望这对您有帮助