1 <html> 2 <head> 3 <meta charset="utf-8"> 4 <title></title> 5 <style type="text/css"> 6 div{width: 100px;height: 100px;} 7 .red{background: red;} 8 .blue{background: blue;} 9 </style> 10 </head> 11 <body> 12 <div id="box" class="red"></div> 13 </body> 14 </html>
<script> window.onload = function(){ var box = document.getElementById("box"); box.onclick = toBlue; } function toBlue(){ this.className = "blue"; this.onclick = toRed; } function toRed(){ this.className = "red"; this.onclick = toBlue; } </script>