实现思路:
- 获取 目标元素(点击它后发生变化的元素)
- 给 目标元素 绑定 事件,选择合适的事件类型,并写明 事件处理程序
- 关键点:点击关闭图标后,设置元素对象的样式- - 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>
<style>
div {
position: relative;
width: 200px;
height: 200px;
margin: 100px auto;
}
i {
position: absolute;
top: 0;
left: 0;
width: 30px;
height: 30px;
background: url(images/叉号.png) center/30px 30px no-repeat;
}
img {
width: 100%;
}
</style>
</head>
<body>
<div class="box">
<i class="close-icon"></i>
<img src="images/皮卡丘.jpg" alt="">
</div>
<script>
var box = document.querySelector('.box');
var close = document.querySelector('.cancel-icon');
cancel.onclick = function() {
box.style.display = 'none';
}
</script>
</body>
</html>