2011-10-12 195 views
0

我需要你的幫助。我有這個JavaScript函數來寫入消息到日誌。日誌文本顏色始終爲藍色。任何人都可以解決這個問題?以下是我的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

試試這個代碼

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

使用'',而不是''對IE的兼容性。 – rid

+0

它現在正在工作。這是我的div,日誌應該顯示'

'。我應該在裏面放嗎? – justin

+0

拉杜和尼蒂斯,現在工作。我只是誤解了刪除功能。非常感謝你的幫助。 – justin

1

的問題是,每次添加一個新的消息的時候,你還影響你對整個#logs而不僅僅是消息。因此,而不是:

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

使用類似:

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