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
will-change tells the browser what changes will h...
Table of contents Implementation ideas: Step 1: C...
The detailed process of using MySQL database with...
Ubuntu Server 16.04 MySQL 8.0 installation and co...
Notes on installing MySQL database, share with ev...
Table of contents WebAPI DOM DOM Tree DOM element...
This article shares with you the installation tut...
This article shares the specific code of JS to re...
Ⅰ. Problem description: Use CSS to achieve 3D con...
Sophie Hardach Clyde Quay Wharf 37 East Soapbox Rx...
This article mainly describes two kinds of underl...
I have several tomcats here. If I use them at the...
Question: What is the difference between int(1) a...
Deploy nginx with docker, it's so simple Just...
Drag and drop is a common function in the front e...