dom啦 10 关闭广告实例 (核心:想要删掉某一个元素就找它自己的父元素,由父元素执行删掉它)

2020-10-28 14:48:05 浏览数 (1)

代码语言:javascript复制
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
		*{padding: 0px;margin: 0px;}
		div
		{
			position: fixed;
			left: 0px;
			bottom: 0px;
		}
		div>img:first-child
		{
			position: absolute;
			right: 0px;
			top: -17px;
		}
	</style>
</head>
<body>
	<div>
	 <img src="images/close.jpg" alt="">
    <img src="images/sina-ad.png" alt="">
</div>
<script type="text/javascript">
	let qq=document.querySelector("div>img:first-child");
	qq.onclick=function()
	{
		//找到img的父元素div
		let ww=this.parentNode;
		//然后找到div的父元素(body)执行删掉div的操作。
		ww.parentNode.removeChild(ww);
	}
</script>
</body>
</html>
dom

0 人点赞