2010-11-29 97 views
0

的函數被調用通過:JavaScript不能設置顏色

myChart.gChangeBarColour(1, "#000000"); 

這工作:

// Changes bars colour 
    this.gChangeBarColour = function(gBarID, gBarColour) { 

     if (gBarID <= this.gData.length && gBarID >= 0) { 

      document.getElementById("gBar" + gBarID).style.backgroundColor = '#000000'; 

     } 

    } 

但是,這並不工作:

// Changes bars colour 
this.gChangeBarColour = function(gBarID, gBarColour) { 

    if (gBarID <= this.gData.length && gBarID >= 0) { 

     document.getElementById("gBar" + gBarID).style.backgroundColor = '" + gBarColour + "'; 

    } 

} 

在沒有錯誤控制檯!有任何想法嗎?

回答

7

您的'" + gBarColour + "'是一個string,用單引號'分隔,其中包含" + gBarColour + ",然後將該值用作顏色。

你需要離開了所有的報價和加號:

// assign the value of gBarColour to the backgroundColor property 
document.getElementById("gBar" + gBarID).style.backgroundColor = gBarColour; 
1
'" + gBarColour + "' 

應該

gBarColour''+gBarColour