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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:29:39  来源:igfitidea点击:

Getting the parent div of element

javascripthtmlgetelementbyid

提问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.parentor parent.containerwould work but I keep getting not definederrors. Note that the pDocis defined, just not certain variables of it.

我会想document.parentparent.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 Elementinherits 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
  • DOM2 核心规范 - 得到所有主流浏览器的良好支持
  • DOM2 HTML规范 - DOM 和 HTML 之间的绑定
  • DOM3 核心规范 - 一些更新,并非所有主流浏览器都支持
  • HTML5 规范- 现在包含 DOM/HTML 绑定

回答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.parentElementor pDoc.parentNodewill get you the parent element.

该属性pDoc.parentElementpDoc.parentNode将为您提供父元素。

回答by LoganDark

var parentDiv = pDoc.parentElement

var parentDiv = pDoc.parentElement

edit: this is sometimes parentNodein 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 -->