简体   繁体   English

JavaScript更改文字颜色

[英]JavaScript change the text color

I need your help. 我需要你的帮助。 I have this javascript function to write a message to the logs. 我具有此javascript函数,可将消息写入日志。 The logs text color is always blue. 日志文本颜色始终为蓝色。 Anyone who can solve this issue? 谁能解决这个问题? Below is my javascript code. 以下是我的JavaScript代码。

Thank you for your help. 谢谢您的帮助。

function logMessage(taskName,action,from,to) 
{
    var $logsDiv = jQuery("#logs");
    var message = '';
    if(action == "receive")
    {
       message = taskName +" was removed from '"+ from +"' and was added to '"+ to +"'<br/>";
       jQuery("#logs").css("color","blue");
       $logsDiv.append(message);
    }
    else
    {
      message = taskName +" was removed from '"+ from +"' and was added to '"+ to +"'<br/>";
      jQuery("#logs").css("color","green");
      $logsDiv.append(message);
    }
 }

Try this code 试试这个代码

if(action == "receive")
{
   message = taskName +" was removed from '"+ from +"' and was added to '"+ to +"'<br/>";
   jQuery("<span>").css("color","blue").html(message).appendTo("#logs");
}
else
{
  message = taskName +" was removed from '"+ from +"' and was added to '"+ to +"'<br/>";
  jQuery("<span>").css("color","green").html(message).appendTo("#logs");
}

The problem is that, each time you add a new message, you also color your entire #logs instead of just the message. 问题在于,每次添加新消息时,您还需要为整个#logs颜色,而不仅仅是消息。 So instead of: 所以代替:

jQuery("#logs").css("color","blue");

use something like: 使用类似:

message = jQuery("<div/>").css("color","blue").append(message);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM