发表于: 2016-08-21 00:58:50
0 2458
hmm first time using this site, there are still lots room to improve I think.
I'm using mac currently and really sleepy now so didn't bother to get the nginx installed.
just gonna post the js code with comments here :D
assuming every single cell is in a div with cell as class name and id from 1 to 9.
execute the following function when onload:
function changeColor () {
for (var j = 1; j < 10; j++) { // reset all the divs before changing color.
var colorDiv = document.getElementById(j);
if (colorDiv.className != "cell") {
colorDiv.className = "cell";
}
}
var randomDiv = document.getElementsByClassName("cell"); // get all the divs.
var i = Math.floor(Math.random() * 9); // get a random div from 1 to 9.
var k = Math.floor(Math.random() * 10); //get a random color.
if (k <= 3) {
randomDiv[i].className = "red";
console.log("格子" + (i + 1) + "变成了红色");
}else if (k <=6) {
randomDiv[i].className = "blue";
console.log("格子" + (i + 1) + "变成了蓝色");
}else {
randomDiv[i].className = "green";
console.log("格子" + (i + 1) + "变成了绿色");
}
setTimeout(changeColor, 1000);
}
评论