Body part: <button>Turn on/off light</button> Script part: <script> // window.onload is the window loading event, which can be used to write code to the element window.addEventListener('load', function () { var btn = document.querySelector('button'); // Define a variable to determine whether the light is on or off var flag = 0; btn.onclick = function () { if (flag == 0) { document.body.style.backgroundColor = 'black'; flag = 1; } else { document.body.style.backgroundColor = 'pink'; flag = 0; } } }) </script> If Then use the following code:
<script>
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;
}
}
</script>
This is the end of this article about using js to implement simple light switch code. For more relevant js to implement simple light switch content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Explain the deployment and configuration of Clickhouse Docker cluster with examples
>>: Summary of a CSS code that makes the entire site gray
This article example shares the specific code for...
This article shares the specific code of JavaScri...
Table of contents background Understanding compos...
To transfer files between the host and the contai...
Basic concepts: Macvlan working principle: Macvla...
1. Background Use LDAP to centrally manage operat...
1. What is HTML HTML (HyperText Markup Language):...
js realizes the special effect of clicking and dr...
Preface Tip: The following is the main content of...
When writing the HTTP module of nginx, it is nece...
1. The proxy_pass directive of the 1.ngx_stream_p...
This article shares the specific code of Vue to i...
This article records the installation tutorial of...
In the previous article, we learned about the net...
Preface In a previous project, the CASE WHEN sort...