通过这个简单的按钮展示出布尔值开关的好处和使用方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>布尔值开关</title>
<style>
.box{
width: 100px;
height: 100px;
background: #eee;
}
</style>
</head>
<body>
<input type="button" value="点击切换颜色">
<div class="box">
</div>
</body>
<script>
var btn = document.querySelector( 'input'),
box = document.querySelector('.box');
var flag = false;
btn.onclick = function(){
if(flag){
box.style.background = '#232323';
flag = false;
}else{
box.style.background = '#292292';
flag = true;
}
}
</script>
</html>
网友评论