【发布时间】:2016-10-16 17:54:29
【问题描述】:
我想用 6 个带有 HTML 和 CSS 的盒子做一个猜谜游戏,然后给每个盒子一个唯一的颜色。
我生成 RGB 中的颜色,但它没有生成数组,数组只有 1,2,3,4,5,6。
var colors = [];
var squares = document.querySelectorAll(".square");
function generateColors (num){
var r = Math.floor(Math.random()* 256);
var g = Math.floor(Math.random()* 256);
var b = Math.floor(Math.random()* 256);
var rgb = "rgb(" + r + ", " + g + ", " + g + ")";
var i = 0;
for(i; i <= num-1; i++){
colors[i] = colors.push(rgb);
}
for(i; i <= num-1; i++){
squares[i].style.background = colors[i];
}
console.log(rgb);
}
generateColors(6);
【问题讨论】:
标签: javascript html css arrays