JavaScript 隔一秒随机生成一种背景色
<html>
<head>
<meta charset="UTF-8">
<style>
div {
position: absolute;
width: 100px;
height: 100px;
top: 30px;
left: 40px;
}
</style>
<script>
function randomNum(firstValue, lastValue) {
var choices = lastValue - firstValue;
return Math.floor(Math.random() * choices + firstValue);
}
function changeColor() {
var colors = new Array("red", "green", "blue", "yellow", "black", "purple", "brown");
return colors[randomNum(0, colors.length - 1)];
}
function divBackgroundColor() {
var div = document.getElementsByTagName("div")[0];
div.style.backgroundColor = changeColor();
}
function timeColor() {
divBackgroundColor();
setTimeout(timeColor, 1000);
}
window.onload = timeColor;
</script>
</head>
<body>
<div>
</div>
</body>
</html>
更多推荐
JavaScript 隔一秒随机生成一种背景色
发布评论