我有这 3 个 div
有没有办法获取id parent
当你点击那个 child 的 child 时,如果是,怎么办?
function getParentId(el) {
//get the id "parent"
}
<div id="parent">
<div class="child">
<div class="child-of-child" onClick="getParentId(this)">
<!-- some code here -->
</div>
</div>
</div>
最佳答案
您可以使用Node.parentNode
:
function getParentId(el){
var p = el.parentNode.parentNode;
console.log(p.id);
}
<div id="parent">
<div class="child">
<div class="child-of-child" onClick="getParentId(this)">
<!-- some code here -->
Click
</div>
</div>
</div>
关于javascript - JS从子对象中获取子对象的父ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51854579/