开关灯效果
代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> img{ display:inline-block; width:170px; height:270px; } </style> </head> <body> <img src="images/bright.jpg" alt=""> <script> var img = document.getElementsByTagName('img')[0]; // 自定义 属性 var flag = true; // 假设 开关flag 值 true-亮 false-灭 img.onclick = function(){ if(flag){ // flag是否为true 是不是亮的 img.src = 'images/dark.jpg'; flag = false; }else{ img.src = 'images/bright.jpg'; flag = true; } } </script> </body> </html>