Home >Web Front-end >JS Tutorial >Code sharing for realizing switch effect using JavaScript

Code sharing for realizing switch effect using JavaScript

巴扎黑
巴扎黑Original
2017-09-09 09:57:292220browse

This article will share with you a simple code to achieve the light switch effect based on js. The code is simple and easy to understand, very good, and has reference value. Friends who need it can refer to it

No more nonsense, I will post the code directly for you. The specific code is as follows:


<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>开关灯</title>
 <style type="text/css">
  html, body {
   margin: 0px;
   padding: 0px;
   width: 100%;
   height: 100%;
   cursor: pointer;
   background-color: white;
  }
 </style>
</head>
<body id="bodyEle">
<script type="text/javascript">
 var oBody = document.getElementById("bodyEle");
 oBody.onclick = function () {
  var bg = this.style.backgroundColor;
  switch (bg) {
   case "white":
    this.style.backgroundColor = "red";
    break;
   case "red":
    this.style.backgroundColor = "black";
    break;
   default:
    this.style.backgroundColor = "white";
  }

 }
</script>
</body>
</html>

The above is the detailed content of Code sharing for realizing switch effect using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn