parentElement
is new to Firefox 9 and to DOM4, but it has been present in all other major browsers for ages.
(parentElement
是Firefox 9和DOM4的新功能,但它已经存在于所有其他主流浏览器中。)
In most cases, it is the same as parentNode
.
(在大多数情况下,它与parentNode
相同。)
The only difference comes when a node's parentNode
is not an element.(唯一的区别在于节点的parentNode
不是元素。)
If so, parentElement
is null
.(如果是,则parentElement
为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
.
(由于<html>
元素( document.documentElement
)没有父元素,因此parentElement
为null
。)
(There are other, more unlikely, cases where parentElement
could be null
, but you'll probably never come across them.)((还有其他更不可能的情况,其中parentElement
可能为null
,但您可能永远不会遇到它们。))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…