开发者

Javascript can't set colour

开发者 https://www.devze.com 2023-01-27 16:41 出处:网络
The function is called via: 开发者_如何学JAVAmyChart.gChangeBarColour(1, \"#000000\"); This works:

The function is called via:

开发者_如何学JAVAmyChart.gChangeBarColour(1, "#000000");

This works:

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

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

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

        }

    }

But this doesn't work:

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

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

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

    }

}

No errors in the console at all! Any ideas?


Your '" + gBarColour + "' is a string , delimited by single quotes ' that contains " + gBarColour + ", that value is then used as the color.

You need to leave out all the quotes and plus signs:

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


'" + gBarColour + "'

should be

gBarColour or ''+gBarColour

0

精彩评论

暂无评论...
验证码 换一张
取 消