4

好吧,基本上,我的一个朋友正在练习 JS,他有一个测试基础站点的想法。所以我说我们将进行一场比赛来完成它。在这一点上,我们都遇到了错误。我们在 JS 中创建了一种颜色。但是,当我们需要输出时,它不起作用。我有这个。

document.getElementById("outputColor").style.backgroundColor=currentColor;

通过这个制作当前颜色的地方

part1 = Math.floor(Math.random() * (255 - 0 + 1)) + 0;
part2 = Math.floor(Math.random() * (255 - 0 + 1)) + 0;
part3 = Math.floor(Math.random() * (255 - 0 + 1)) + 0;
currentColor = "\"rgb (" + part1 + ", " + part2 + ", " + part3 + ")\"";

将当前颜色放在“”中意味着它期待 currentColor 的值。不是实际的变量值。

希望这是有道理的。这是可能的,还是我们找错了树?

谢谢

编辑:它确实有一个与之相关的 CSS 样式

#outputColor
{
    height: 100px;
    width: 100px;
    background-color: rgb(0,0,0);
}

编辑:已解决,解决方案是

currentColor = "rgb(" + part1 + ", " + part2 + ", " + part3 + ")";

谢谢你们!

4

3 回答 3

4

双引号太多了,用这个:

currentColor = "rgb(" + part1 + ", " + part2 + ", " + part3 + ")";
于 2012-06-16T22:35:54.813 回答
1
currentColor = "rgba(" + part1 + ", " + part2 + ", " + part3 + ",0)";
于 2012-06-16T22:38:35.940 回答
1
currentColor = "rgb(" + part1 + ", " + part2 + ", " + part3 + ")"; // RGB

或使用十六进制格式

currentColorHex="#"+(part1).toString(16)+(part2).toString(16)+(part3).toString(16);

演示。

于 2012-06-16T22:39:05.180 回答