<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> 显示和隐藏案例 </title>
<style>
.box {
width: 300px;
height: 300px;
background-color: rgb(238, 6, 6);
margin-top: 10px;
}
.hide {
display: none;
}
.box1 {
width: 300px;
height: 300px;
background-color: rgb(238, 6, 6);
margin-top: 10px;
}
.hide1 {
display: none;
}
</style>
</head>
<body>
<!-- 初级显示和隐藏案例 -->
<input type="button" value="显示" id="show">
<input type="button" value="隐藏" id="hide">
<div class="box"></div>
<hr>
<!-- 高级显示和隐藏案例 -->
<input type="button" value="隐藏" id="hide1">
<div class="box1"></div>
<script>
// 初级显示和隐藏案例
// 找 显示 / 隐藏 按钮;
var show = document.getElementById('show');
var hide = document.getElementById('hide');
// 找div
var box = document.getElementsByClassName('box')[0];
//隐藏的点击事件
hide.onclick = function () {
box.className += " hide";
}
//显示的点击事件
show.onclick = function () {
box.className = "box"
}
// ------------------------------------------------
// 高级显示和隐藏案例
var hide1 = document.getElementById('hide1');
// 找div
var box1 = document.getElementsByClassName('box1')[0];
//点击事件
hide1.onclick = function () {
if (hide1.value == "隐藏") {
box1.className += " hide1";
hide1.value = "显示";
} else {
box1.className = "box1";
hide1.value = "隐藏";
}
}
</script>
</body>
</html>
网友评论