廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
<!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>總結
以上所述是小編給大家介紹的JavaScript實現開關等效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答