Javascript 获取元素的父div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6856871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the parent div of element
提问by OVERTONE
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. Note that the pDoc
is defined, just not certain variables of it.
我会想document.parent
或parent.container
会工作,但我不断收到not defined
错误。请注意,pDoc
是定义的,只是不是它的某些变量。
Any ideas?
有任何想法吗?
P.S. I would prefer to avoid jQuery if possible.
PS 如果可能的话,我宁愿避免使用 jQuery。
回答by T.J. Crowder
You're looking for parentNode
, which Element
inherits from Node
:
您正在寻找parentNode
,它Element
继承自Node
:
parentDiv = pDoc.parentNode;
Handy References:
方便参考:
- DOM2 Corespecification - well-supported by all major browsers
- DOM2 HTMLspecification - bindings between the DOM and HTML
- DOM3 Corespecification - some updates, not all supported by all major browsers
- HTML5 specification- which now has the DOM/HTML bindings in it
回答by RobG
If you are looking for a particular type of element that is further away than the immediate parent, you can use a function that goes up the DOM until it finds one, or doesn't:
如果您正在寻找比直接父元素更远的特定类型的元素,您可以使用一个函数,该函数在 DOM 中向上移动直到找到一个,或者没有:
// Find first ancestor of el with tagName
// or undefined if not found
function upTo(el, tagName) {
tagName = tagName.toLowerCase();
while (el && el.parentNode) {
el = el.parentNode;
if (el.tagName && el.tagName.toLowerCase() == tagName) {
return el;
}
}
// Many DOM methods return null if they don't
// find the element they are searching for
// It would be OK to omit the following and just
// return undefined
return null;
}
回答by Thor Jacobsen
The property pDoc.parentElement
or pDoc.parentNode
will get you the parent element.
该属性pDoc.parentElement
或pDoc.parentNode
将为您提供父元素。
回答by LoganDark
var parentDiv = pDoc.parentElement
var parentDiv = pDoc.parentElement
edit: this is sometimes parentNode
in some cases.
编辑:这有时是parentNode
在某些情况下。
https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement
https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement
回答by Sinan ?ALI?KAN
This might help you.
这可能对你有帮助。
ParentID = pDoc.offsetParent;
alert(ParentID.id);
回答by vish0910
Knowing the parent of an element is useful when you are trying to position them out the "real-flow" of elements.
当您尝试将元素定位在元素的“真实流”之外时,了解元素的父级很有用。
Below given code will output the id of parent of element whose id is provided. Can be used for misalignment diagnosis.
下面给定的代码将输出提供 id 的元素的父元素的 id。可用于错位诊断。
<!-- Patch of code to find parent -->
<p id="demo">Click the button </p>
<button onclick="parentFinder()">Find Parent</button>
<script>
function parentFinder()
{
var x=document.getElementById("demo");
var y=document.getElementById("*id of Element you want to know parent of*");
x.innerHTML=y.parentNode.id;
}
</script>
<!-- Patch ends -->