问题:
This should be really simple but I'm having trouble with it.这应该很简单,但我遇到了麻烦。 How do I get a parent div of a child element?如何获取子元素的父 div?
My HTML:我的 HTML:
<div id="test">
<p id="myParagraph">Testing</p>
</div>
My JavaScript:我的 JavaScript:
var pDoc = document.getElementById("myParagraph");
var parentDiv = ??????????
I would have thought document.parent
or parent.container
would work but I keep getting not defined
errors.我原以为document.parent
或parent.container
会工作,但我不断收到not defined
错误。 Note that the pDoc
is defined, just not certain variables of it.请注意, pDoc
已定义,但不是它的某些变量。
Any ideas?有任何想法吗?
PS I would prefer to avoid jQuery if possible. PS 如果可能的话,我宁愿避免使用 jQuery。
解决方案:
参考一: https://stackoom.com/question/Slmh参考二: Getting the parent div of element