js实战案例–点击关闭二维码
要求:当鼠标点击二维码关闭按钮时,则关闭整个二维码
如👇图
![某不知名代码搬运工](https://img-blog.csdnimg.cn/ee98a423090b44ad814d61953577af7a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p-Q5LiN55-l5ZCN5Luj56CB5pCs6L-Q5bel,size_6,color_FFFFFF,t_70,g_se,x_16)
思路:
- 利用样式的显示(display:block)和隐藏(display:none)完成。
- 点击按钮,隐藏二维码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.box{
position: relative;
width: 74px;
height: 88px;
border: 1px solid #ccc;
margin: 100px auto;
font-size: 12px;
text-align: center;
color: #f40;
}
.box img{
width: 90px;
margin-top: 5px;
}
.atn {
position: absolute;
top: -1px;
left: -16px;
width: 14px;
height: 14px;
border: 1px solid #ccc;
line-height: 14px;
cursor: pointer;
}
</style>
<body>
<div class="box">
二维码
<img src="images/tao.png" alt="">
<i class="atn">x</i>
</div>
<script>
var atn = document.querySelector('.atn');
var box = document.querySelector('.box');
atn.onclick = function(){
box.style.display = 'none';
}
</script>
</body>
</html>