Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

javascript - DOM parentNode和parentElement之间的区别(Difference between DOM parentNode and parentElement)

有人可以用尽可能简单的方式解释我,经典DOM parentNode和Firefox 9中新引入的内容有什么区别parentElement

  ask by shabunc translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 .

(如果是,则parentElementnull 。)

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 )没有父元素,因此parentElementnull 。)

(There are other, more unlikely, cases where parentElement could be null , but you'll probably never come across them.)

((还有其他更不可能的情况,其中parentElement可能为null ,但您可能永远不会遇到它们。))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...