<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .stage{ width: 210px; height: 250px; margin: 50px auto; } ...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.stage{
width: 210px;
height: 250px;
margin: 50px auto;
}
#box{
margin-top: 20px;
width: 210px;
height:210px;
background: red;
}
</style>
</head>
<body>
<div class="stage">
<span>點擊盒子變換顏色額!!</span>
<div id="box" onclick="changecolor()"></div>
</div>
</body>
<script type="text/javascript">
var box=1;
function changecolor(){
var a=document.getElementById("box");
switch(box){
case 1:a.style.backgroundColor="yellow";box++;break;
case 2:a.style.backgroundColor="cyan";box++;break;
case 3:a.style.backgroundColor="tomato";box++;break;
case 4:a.style.backgroundColor="purple";box++;break;
case 5:a.style.backgroundColor="cornflowerblue";box++;break;
case 6:a.style.backgroundColor="darksalmon";box++;break;
case 7:a.style.backgroundColor="red";box=1;break;
}
}
</script>
</html>