This article will share with you a simple code to achieve the light switch effect based on js. The code is simple and easy to understand, very good, and has reference value. Friends who need it can refer to it
No more nonsense, I will post the code directly for you. The specific code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>开关灯</title> <style type="text/css"> html, body { margin: 0px; padding: 0px; width: 100%; height: 100%; cursor: pointer; background-color: white; } </style> </head> <body id="bodyEle"> <script type="text/javascript"> var oBody = document.getElementById("bodyEle"); oBody.onclick = function () { var bg = this.style.backgroundColor; switch (bg) { case "white": this.style.backgroundColor = "red"; break; case "red": this.style.backgroundColor = "black"; break; default: this.style.backgroundColor = "white"; } } </script> </body> </html>
The above is the detailed content of Code sharing for realizing switch effect using JavaScript. For more information, please follow other related articles on the PHP Chinese website!