js随机模块颜色

<!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>

 

posted @ 2017-02-17 16:47  Monroe_Yu  阅读(174)  评论(0编辑  收藏  举报