简体   繁体   English

获取元素的父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。

You're looking for parentNode , which Element inherits from Node :您正在寻找parentNode ,其中Element继承自Node

parentDiv = pDoc.parentNode;

Handy References:方便参考:

  • DOM2 Core specification - well-supported by all major browsers DOM2 核心规范 - 得到所有主流浏览器的良好支持
  • DOM2 HTML specification - bindings between the DOM and HTML DOM2 HTML规范 - DOM 和 HTML 之间的绑定
  • DOM3 Core specification - some updates, not all supported by all major browsers DOM3 核心规范 - 一些更新,并非所有主流浏览器都支持
  • HTML5 specification - which now has the DOM/HTML bindings in it HTML5 规范- 现在包含 DOM/HTML 绑定

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;
}

Edit 2021编辑 2021

Element.closest is part of the DOM standard . Element.closestDOM 标准的一部分。 It takes a selector as an argument and returns the first matching ancestor or null if there isn't one.它将选择器作为参数并返回第一个匹配的祖先,如果没有则返回 null。

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

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

This might help you.这可能会帮助你。

ParentID = pDoc.offsetParent;
alert(ParentID.id); 

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.下面给出的代码将输出提供了 id 的元素的父级的 id。 Can be used for misalignment diagnosis.可用于错位诊断。

<!-- 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 -->

I had to do recently something similar, I used this snippet:我最近不得不做类似的事情,我使用了这个片段:

const getNode = () =>
  for (let el = this.$el; el && el.parentNode; el = el.parentNode){
    if (/* insert your condition here */) return el;
  }
  return null
})

The function will returns the element that fulfills your condition.该函数将返回满足您条件的元素。 It was a CSS class on the element that I was looking for.这是我正在寻找的元素上的 CSS 类。 If there isn't such element then it will return null如果没有这样的元素,那么它将返回null

In case somebody would look for multiple elements it only returns closest parent to the element that you provided.如果有人会寻找多个元素,它只会返回与您提供的元素最接近的父元素

My example was:我的例子是:

if (el.classList?.contains('o-modal')) return el;

I used it in a vue component (this.$el) change that to your document.getElementById function and you're good to go.我在一个 vue 组件(this.$el)中使用了它,将它更改为你的document.getElementById函数,你就可以开始了。 Hope it will be useful for some people ✌️希望对一些人有用✌️

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM