js控制开关灯泡
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title>
</head>
<body><img src="./image/off.png" alt="" id="image"><button id="turn">开关</button><script>var image = document.getElementById("image");var turn = document.getElementById("turn");var flag = true;turn.onclick = function () {if (flag){image.src = "./image/on.png";flag = false;}else {image.src = "./image/off.png";flag = true;}}</script>
</body>
</html>
注意:自己找两张图片,一张是关灯的图片,一张是开灯的图片。引入时图片路径和名字注意一下。