效果:
代码:
结构:
<div class="box">
<img src="images/taobao.jpg" alt="" />
<span id="x">×</span>
</div>
样式
<style>
.box {
border: 1px solid #D9D9D9;
margin: 100px auto;
position: relative;
width: 107px;
}
#x {
border: 1px solid #D9D9D9;
width: 14px;
height: 14px;
line-height: 14px;
color: #D6D6D6;
cursor: pointer;
position: absolute;
top: 0;
left: -15px;
}
</style>
方法一(script部分):
let box = document.querySelector('.box')
let x = document.querySelector('#x')
x.onclick = function () {
box.style.display = 'none'
}
方法二(script部分):
let x = document.querySelector('#x')
x.onclick = function () {
this.parentElement.style.display = 'none'
}