<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <Title> lamp switch </ title> </head> <body> <img id="light" src="img/off.gif" alt=""> <script> /* * Analysis * 1. Get Object * 2. binding events * 3. Set Event: Each click switches the picture * Rules: On - Off, Off - On * Use the flag to complete * * / // 1. Get the object var Light = document.getElementById ( "Light" ); var In Flag = to false ; // the representative lamp is off when // 2. Binding Events light.onclick = function () { IF ( in Flag) { // lamp is oN exterminate light.src = "IMG / off.gif" ; flag=false; } The else { // the lamp is off to open light.src = "IMG / on.gif" ; flag=true; } } </script> </body> </html>