05.02用javascript改变div的颜色

调用javascript改变div的颜色

<!DOCTYPE html>
<html lang="zh-cn">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript">
	function randcolor(){
		var c = new Array();
		var dbgc;
		for(var i = 0 ; i < 3 ; i++ )
			c[i]=Math.floor(Math.random()*256);
		dbgc = "rgb("+c[0]+","+c[1]+","+c[2]+")";
		document.getElementById("play").style.backgroundColor=dbgc;
	}
	</script>
	<style>
		body{
			margin: 0 ;
			padding: 0 ;
		}
		#play{
			width: 100%;
			height: 200px;
			background-color:blue;
		}
	</style>
</head>
<body>
		<div id="play" onclick="randcolor()"></div>
</body>
</html>
			
			 
<!DOCTYPE html>
<html lang="zh-cn">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		body{
			margin: 0 ;
			padding: 0 ;
		}
		#play{
			width: 100%;
			height: 200px;
			background-color:blue;
		}
	</style>	
</head>
<body>
	<div id="play"></div>
	<script>
		function randcolor(){
			var c = new Array();
			for (var i = 0 ; i < 3 ; i++ )
				c[i]=Math.floor(Math.random()*256);
			return "rgb("+c[0]+","+c[1]+","+c[2]+")";
		}
		var play = document.getElementById("play");
		play.onclick=function(){
			play.style.backgroundColor=randcolor();
		}
	</script>
</body>
</html>

你可能感兴趣的:(初学,javascript,html,js)