<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 500px;
height: 400px;
position: relative;
margin: 50px auto;
display: none;
}
img {
width: 500px;
height: 400px;
}
span {
position: absolute;
right: 20px;
top: 20px;
background: #ccc;
}
</style>
</head>
<body>
<div><img src="./img/1.jpg" alt=""><span>X</span></div>
<script>
// 等待页面3s后,div显示出来
var div = document.getElementsByTagName('div')[0];
setTimeout(function () {
div.style.display = 'block';
}, 3000);
// 点击span标签,让div隐藏
var span = document.getElementsByTagName('span')[0];
span.onclick = function () {
div.style.display = 'none';
// 等待3s让div显示出来
setTimeout(function () {
div.style.display = 'block';
}, 3000);
}
</script>
</body>
</html>