Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use js to switch on and off lights

Shulou Source: shulou.com Published: 2022-06-02 11:56:15 10月02日 Update

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!

Tags: Articles code content parts not much events elements variables most situations more knowledge industry information information channels facets channels references learning help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info Shulou Information Xiaomi MariaDB