DOM parentNode和parentElement之间的区别 [英] Difference between DOM parentNode and parentElement

查看:193
本文介绍了DOM parentNode和parentElement之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用尽可能简单的说明来解释我,古典DOM parentNode ,最近在Firefox 9中引入 parentElement

解决方案

parentElement Firefox 9和DOM4,但它已经存在于所有其他主流浏览器中。



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



示例:

  document.body.parentNode; //< html>元素
document.body.parentElement; //< html>元素

document.documentElement.parentNode; //文档节点
document.documentElement.parentElement; // null

由于< html> 元素( document.documentElement )没有父元素是一个元素, parentElement 。 (还有其他更不可能的情况,其中 parentElement 可以是 null ,但是你可能永远不会遇到他们。)


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

解决方案

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

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之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆