【问题标题】:Difference between DOM parentNode and parentElementDOM parentNode 和 parentElement 的区别
【发布时间】:2011-12-31 01:50:09
【问题描述】:

有人可以简单解释一下,经典 DOM parentNode 和 Firefox 9 中新引入的 parentElement 有什么区别

【问题讨论】:

标签: javascript firefox dom


【解决方案1】:

parentElement 是 Firefox 9 和 DOM4 的新功能,但它已在所有其他主要浏览器中出现了很长时间。

在大多数情况下,它与parentNode 相同。唯一的区别在于节点的parentNode 不是元素。如果是这样,parentElement 就是 null

举个例子:

document.body.parentNode; // the <html> element
document.body.parentElement; // the <html> element

document.documentElement.parentNode; // the document node
document.documentElement.parentElement; // null

(document.documentElement.parentNode === document);  // true
(document.documentElement.parentElement === document);  // false

由于 &lt;html&gt; 元素 (document.documentElement) 没有作为元素的父元素,因此 parentElementnull。 (还有其他更不可能的情况,parentElement 可能是 null,但您可能永远不会遇到它们。)

【讨论】:

  • 换句话说,99.999999999999% 的时间完全没有意义。是谁的主意?
  • 原来的parentElement是IE专有的东西;我相信当时的其他浏览器(例如 Netscape)支持parentNode,但不支持parentElement。 (显然,鉴于我已经提到了 Netscape,我说的是回到 IE5 及更早版本......)
  • @lonesomeday 你忘了documentfragment.firstChild.parentElement === null
  • @Raynos 这实际上是我在回答最后一句话时想到的确切情况......
  • 正如我刚刚发现的那样,在 SVG 元素上(例如 g 内的 circle),在 IE 中,parentElement 将是未定义的,而 parentNode 将是你的重新寻找。 :(
【解决方案2】:

在 Internet Explorer 中,parentElement 未为 SVG 元素定义,而 parentNode 已定义。

【讨论】:

  • 老实说,我认为这更像是一个评论而不是一个答案。
  • 可能,但这就是我把头撞在桌子上一个小时或更长时间直到我弄明白的原因。我怀疑许多其他人在类似的头撞后来到这个页面。
  • @speedplane 很高兴这是一个答案,因为这没有逻辑意义,让我难过了好一阵子......
  • 评论节点也未定义。在 Chrome 中,我很高兴获得评论的父级,但在 IE 中未定义。
  • 我找不到那个来源。 parentElement 没有为 Node 实现是众所周知的 (developer.mozilla.org/en-US/docs/Web/API/Node/…) 但对于 SVGElement?我也无法在 IE 11.737.17763.0 中使用 document.createElement('svg').parentElement 重现此内容。这可能是在此期间解决的吗?
【解决方案3】:

使用.parentElement,只要不使用文档片段就不会出错。

如果你使用文档片段,那么你需要.parentNode:

let div = document.createDocumentFragment().appendChild(document.createElement('div'));
div.parentElement // null
div.parentNode // document fragment

还有:

let div = document.getElementById('t').content.firstChild
div.parentElement // null
div.parentNode // document fragment
&lt;template id="t"&gt;&lt;div&gt;&lt;/div&gt;&lt;/template&gt;

显然&lt;html&gt;.parentNode 链接到Document。这应该被视为决策 phail,因为文档不是节点,因为节点被定义可以被文档包含,而文档不能被文档包含。

【讨论】:

    【解决方案4】:

    就像nextSibling and nextElementSibling 一样,请记住,名称中带有“元素”的属性总是返回Elementnull。没有的属性可以返回任何其他类型的节点。

    console.log(document.body.parentNode, "is body's parent node");    // returns <html>
    console.log(document.body.parentElement, "is body's parent element"); // returns <html>
    
    var html = document.body.parentElement;
    console.log(html.parentNode, "is html's parent node"); // returns document
    console.log(html.parentElement, "is html's parent element"); // returns null
    

    【讨论】:

    • 是的,但不像 nextsibling 文本或评论节点不能是父节点。
    【解决方案5】:

    还有一个区别,但仅在 Internet Explorer 中。当您混合 HTML 和 SVG 时会发生这种情况。如果父级是这两个中的“另一个”,则 .parentNode 给出父级,而 .parentElement 给出 undefined。

    【讨论】:

    • 我认为是undefined 而不是null
    猜你喜欢
    • 2013-08-01
    • 2015-06-14
    • 2017-06-24
    • 2011-05-05
    • 2011-09-18
    • 2012-11-02
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多