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 + "';
}
}
控制台中根本没有错误!有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
'" + gBarColour + "'
是一个string
,由单引号'
分隔,其中包含" + gBarColour + ",然后将该值用作颜色。
您需要省略所有引号和加号:
Your
'" + gBarColour + "'
is astring
, 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:
应为
gBarColour
或''+gBarColour
should be
gBarColour
or''+gBarColour