js随机模块颜色

时间:2024-01-17 23:37:08
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312"/>
<title>js </title>
</head>
<body>
<script type="text/javascript"> function getcl(){
var arr = []
i =0;
C = '0123456789ABCDEF';
while(i++ < 6){
x=Math.random()*16;
b=parseInt(x);
c=C.substr(b,1);
arr.push(c);
}
var cl = "#"+ arr.join('');
return cl;
}
document.write(cl);
function setColor(){
document.getElementById("myDiv").style.backgroundColor = getcl();
}
</script>
<div id="myDiv" style="width:200px;height:200px;"></div>
<input type="button" value="myDiv"
onclick="setColor()" />
</body>
</html>