简体   繁体   English

Javascript无法设置颜色

[英]Javascript can't set colour

The function is called via: 该函数通过以下方式调用:

myChart.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. 您的'" + gBarColour + "'是一个string ,由包含" + gBarColour + "的单引号'分隔,然后将该值用作颜色。

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 gBarColour''+gBarColour

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM