這次帶給大家JS怎麼達成開關效果,JS實現開關效果的注意事項有哪些,下面就是實戰案例,一起來看一下。
使用JavaScript實現開關效果也是很多人會遇到的操作,這次文章就給大家簡單的介紹下方法,感興趣的一起來看看。
<!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>我相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
以上是JS怎麼實現開關效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!