获得元素的父div(javascript) [英] Getting parent div of element (javascript)
本文介绍了获得元素的父div(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这应该很简单,但我遇到了麻烦。如何从子元素中获取父div。
This should be really simple but I'm having trouble with it. How do I get a parent div from a child element.
例如
<div id="test>
<p id="myParagraph">Testing</p>
</div>
Javascript
b $ b
Javascript
var pDoc = document.getElementById("myParagraph");
var parentDiv = ??????????
我会认为 document.parent
或 parent.container
会工作,但我会收到未定义的
错误注意 pDoc
I would have thought document.parent
or parent.container
would work but I keep getting not defined
errors. Note that the pDoc
is defined, just not certain variables of it.
任何想法?
PS我希望尽可能避免JQuery。
P.S. I would prefer to avoid JQuery if possible.
推荐答案
您正在寻找 parentNode
,其中 元素
继承自 节点
:
You're looking for parentNode
, which Element
inherits from Node
:
parentDiv = pDoc.parentNode;
方便的参考资料:
- DOM2核心规范 - 得到所有主要浏览器的支持
- DOM2 HTML 规范 - DOM和HTML之间的绑定
-
- DOM3核心规范 - 一些更新,不是全部支持所有主要浏览器
- HTML5规范 - 现在已具有DOM / HTML绑定
- DOM2 Core specification - well-supported by all major browsers
- DOM2 HTML specification - bindings between the DOM and HTML
- DOM3 Core specification - some updates, not all supported by all major browsers
- HTML5 specification - which now has the DOM/HTML bindings in it
这篇关于获得元素的父div(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文