问题描述
谁能简单解释一下,经典DOM与parentNode 并在 Firefox 9 中新引入 parentElement
Can somebody explain in simple terms, what is the difference between classical DOM parentNode and newly introduced in Firefox 9 parentElement
推荐答案
parentElement
是 Firefox 9 和 DOM4 的新增功能,但它已经存在于所有其他主要浏览器中多年.
parentElement
is new to Firefox 9 and to DOM4, but it has been present in all other major browsers for ages.
在大多数情况下,它与 parentNode
相同.唯一的区别是当节点的 parentNode
不是元素时.如果是这样,parentElement
为 null
.
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
.
举个例子:
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
由于 元素 (
document.documentElement
) 没有作为元素的父元素,parentElement
是 .(还有其他更不可能的情况,其中 parentElement
可能是 null
,但您可能永远不会遇到它们.)
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.)
这篇关于DOM parentNode 和 parentElement 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!