【问题标题】:Changing color of text color based on value switch case statement根据值 switch case 语句更改文本颜色的颜色
【发布时间】:2019-11-25 19:52:04
【问题描述】:

如何更改表格中显示的值的颜色,以不同的颜色,例如 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);

【问题讨论】:

  • 您可以为每个优先级创建一个类。
  • 因为你的例子对我来说没有意义,这里有一个关于如何用 jQuery 改变文本颜色的一般方法:$(element).css('color', 'red');

标签: javascript jquery css html-table


【解决方案1】:

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

或者

如果文本颜色是预定义的。您可以为颜色创建不同的类,并根据 switch 更新 className

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • tq 先生,您的回答对我有帮助
    【解决方案3】:
    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);
    

    【讨论】:

      【解决方案4】:

      试试类似

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

      然后

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

      希望对你有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-02
        • 2017-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-31
        • 2023-01-12
        相关资源
        最近更新 更多