JavaScript 改变文本颜色

发布于 2024-12-09 06:03:05 字数 686 浏览 0 评论 0原文

我需要你的帮助。我有这个 javascript 函数可以将消息写入日志。日志文本颜色始终为蓝色。谁能解决这个问题吗?下面是我的 JavaScript 代码。

感谢您的帮助。

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

I need your help. I have this javascript function to write a message to the logs. The logs text color is always blue. Anyone who can solve this issue? Below is my javascript code.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-12-16 06:03:05

试试这个代码

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

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");
}
萌梦深 2024-12-16 06:03:05

问题是,每次添加新消息时,您还会对整个 #logs 进行着色,而不仅仅是消息。因此,不要使用:

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

使用类似以下内容:

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

The problem is that, each time you add a new message, you also color your entire #logs instead of just the message. So instead of:

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

use something like:

message = jQuery("<div/>").css("color","blue").append(message);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文