ChatGPT解决这个技术问题 Extra ChatGPT

Difference between DOM parentNode and parentElement

Can somebody explain in simple terms, what is the difference between classical DOM parentNode and newly introduced in Firefox 9 parentElement

parentNode seems to be DOM standard, so it is safer always use it instead of parentElement.
@TMS w3school is not an authority: w3fools.com
@GuillaumeMassé - Have a new look on w3fools. They now say that the errors/omissions they listed have been corrected.

S
Simon_Weaver

parentElement is new to Firefox 9 and to DOM4, but it has been present in all other major browsers for ages.

In most cases, it is the same as parentNode. The only difference comes when a node's parentNode is not an element. If so, parentElement is null.

As an example:

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

Since the <html> element (document.documentElement) doesn't have a parent that is an element, parentElement is null. (There are other, more unlikely, cases where parentElement could be null, but you'll probably never come across them.)


In other words, it's completely pointless 99.999999999999% of the time. Whose idea was it?
The original parentElement was a proprietary IE thing; I believe other browsers at the time (e.g., Netscape) supported parentNode but not parentElement. (Obviously, given I've mentioned Netscape, I'm talking about way back to IE5 and earlier...)
@lonesomeday you forgot documentfragment.firstChild.parentElement === null
@Raynos That was actually the precise circumstance I had in mind with the last sentence of my answer...
As I have just discovered, on an SVG element (like a circle inside a g), in IE, parentElement will be undefined, and parentNode will be what you're looking for. :(
s
speedplane

In Internet Explorer, parentElement is undefined for SVG elements, whereas parentNode is defined.


honestly I think this is more of a comment rather than an answer.
Probably, but it's the reason I banged my head against the table for an hour or more until I figured it out. I suspect many others come to this page after a similar head-banging.
@speedplane Glad this is an answer as this makes no logical sense and had me stumped for a good while...
It also undefined for comment nodes. In Chrome I was happily getting the parent of a comment, but it was undefined in IE.
I could not find a source for that. parentElement not being implemented for Node is well know (developer.mozilla.org/en-US/docs/Web/API/Node/…) but for SVGElement? I could also not reproduce this with document.createElement('svg').parentElement in IE 11.737.17763.0. Was this maybe fixed in the meantime?
S
Sam

Use .parentElement and you can't go wrong as long as you aren't using document fragments.

If you use document fragments, then you need .parentNode:

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

Also:

let div = document.getElementById('t').content.firstChild div.parentElement // null div.parentNode // document fragment

Apparently the <html>'s .parentNode links to the Document. This should be considered a decision phail as documents aren't nodes since nodes are defined to be containable by documents and documents can't be contained by documents.


B
Buksy

Just like with nextSibling and nextElementSibling, just remember that, properties with "element" in their name always returns Element or null. Properties without can return any other kind of node.

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

yeah, but unlike nextsibling text or comment nodes can't be parent.
m
mathheadinclouds

there is one more difference, but only in internet explorer. It occurs when you mix HTML and SVG. if the parent is the 'other' of those two, then .parentNode gives the parent, while .parentElement gives undefined.


I think it's undefined not null.