定义和用法
parentNode 属性可返回某节点的父节点。
如果指定的节点没有父节点则返回 null 。
所有主要浏览器都支持 parentNode 属性
语法
node.parentNode技术细节
| 返回值: | 作为一个节点对象返回父节点。 |
|---|---|
| DOM 版本 | Core Level 1 Node Object |
实例
返回<li> 元素父节点:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>
<p>示例列表:</p>
<ul><li>Coffee</li><li>Tea</li></ul>
<p id="demo">单击按钮获取第一个列表项的父节点的节点名称。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
var x=document.getElementById("demo");
var y=document.getElementsByTagName("LI")[0];
x.innerHTML=y.parentNode.nodeName;
}
</script>
</body>
</html>作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/4496.html
支付宝
微信