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
How to determine what this points to? ①When calle...
Recently, due to the need to test security produc...
This article shares the installation and configur...
Combining the various problems I encountered in m...
In actual development or production environments,...
>>>>>Ubuntu installation and confi...
<template> <div class="app-containe...
Code implementation: Copy code The code is as fol...
Phenomenon The system could compile the Linux sys...
Just 15 lines of CSS to crash your iPhone Securit...
Problem Description There is a type of query call...
When the Docker container exits, the file system ...
1. Window -> preferences to open the eclipse p...
Table of contents 1. setState() Description 1.1 U...
If you want the path following the domain name to...