javascript - DOM parentNode 和 parentElement 的区别

标签 javascript firefox dom

谁能简单的解释一下,经典DOM parentNode有什么区别?并在 Firefox 9 parentElement 中新引入

最佳答案

parentElement对 Firefox 9 和 DOM4 来说是新的,但它在所有其他主要浏览器中已经存在很长时间了。

在大多数情况下,它与 parentNode 相同。 .唯一的区别在于节点的 parentNode不是一个元素。如果是,parentElementnull .

举个例子:

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

自从 <html>元素 ( document.documentElement ) 没有作为元素的父级 parentElementnull . (还有其他更不可能的情况,其中 parentElement 可能是 null,但您可能永远不会遇到它们。)

关于javascript - DOM parentNode 和 parentElement 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8685739/

相关文章:

html - 在 Firefox 的文本区域内包装文本的行为

http - 需要JS文本!插件和加载超时

javascript - jquery 函数调用不起作用

javascript - Function.prototype.apply 给我留下了 undefined is not a function 错误

javascript - 将特定文本更改为点

javascript - 全局 JSON 变量

Javascript removeChild 不删除循环内的所有元素

javascript - ReactJS(jsx) 传递参数

javascript - Firebug - 如何运行多行脚本或创建新的 JavaScript 文件?

jQuery:动态追加元素+ .on()