javascript基础--DOM选取父节点
一、parentNode
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
window.onload = function() {
var aA = document.getElementsByTagName('a');
var i = 0;
for (i = 0; i < aA.length; i++) {
aA[i].onclick = function() {
this.parentNode.style.display = "none"; //parentNode取父节点
}
}
}
</script>
</head>
<body>
<ul id="ul1">
<li>111<a href="javascript:;">删除</a></li>
<li>222<a href="javascript:;">删除</a></li>
<li>333<a href="javascript:;">删除</a></li>
</ul>
</body>
</html>
二、offsetParent(选取关联的父级)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="div1" style="width:100px;height:100px;background:red;margin:10px;position:relative;">
<!--offsetParent是找他关联的父级-->
<div id="div2" onclick="alert(this.offsetParent.id)" style="width:100px;height:100px;background:yellow;position:absolute;top:100px;left:100px;"></div>
</div>
</body>
</html>

浙公网安备 33010602011771号