JavaScript更改文本颜色

时间:2011-10-12 17:22:01

标签: javascript jquery-selectors

我需要你的帮助。我有这个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);
    }
 }

2 个答案:

答案 0 :(得分:2)

试试此代码

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

答案 1 :(得分:1)

问题是,每次添加新邮件时,您都会为整个#logs而不仅仅是邮件着色。所以而不是:

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

使用类似的东西:

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