獲取元素的父div - Getting the parent div of element

問題:

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.parentparent.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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章