How to use js to switch on and off lights
Editor to share with you how to use js to switch on and off lights, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Body section:
Turn the light on and off
Script section:
Btn.onclick = function () {if (flag = = 0) {document.body.style.backgroundColor = 'black'; flag = 1;} else {document.body.style.backgroundColor =' pink'; flag = 0 })
If script is written directly under button
The following code is used:
Var btn = document.querySelector ('button'); var flag = 0; btn.onclick = function () {if (flag = = 0) {document.body.style.backgroundColor =' black'; flag = 1;} else {document.body.style.backgroundColor = 'pink'; flag = 0 }} above are all the contents of the article "how to use js to switch lights on and off". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!