sha*_*unc 450 javascript firefox dom
有人可以用尽可能简单的方式解释我,经典DOM parentNode和Firefox 9中新引入的内容有什么区别parentElement
lon*_*day 436
parentElement 它是Firefox 9和DOM4的新功能,但它已经存在于所有其他主流浏览器中.
在大多数情况下,它是相同的parentNode.唯一的区别在于节点parentNode不是元素.如果是的话,parentElement是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
Run Code Online (Sandbox Code Playgroud)
由于<html>element(document.documentElement)没有父元素,因此parentElement是null.(还有其他的,更不可能的情况parentElement可能null,但你可能永远不会遇到它们.)
spe*_*ane 87
在Internet Explorer中,parentElement未定义SVG元素,而已parentNode定义.
Pac*_*ier 11
使用.parentElement,只要您不使用文档片段,就不会出错.
如果您使用文档片段,则需要.parentNode:
let div = document.createDocumentFragment().appendChild(document.createElement('div'));
div.parentElement // null
div.parentNode // document fragment
Run Code Online (Sandbox Code Playgroud)
也:
let div = document.getElementById('t').content.firstChild
div.parentElement // null
div.parentNode // document fragmentRun Code Online (Sandbox Code Playgroud)
<template id="t"><div></div></template>Run Code Online (Sandbox Code Playgroud)
显然是文档<html>的.parentNode链接.这应该被认为是决定phail的文件是不是因为节点的节点被定义为通过文件和文件中可容纳不能通过文件包含.
Buk*_*ksy 10
就像nextSibling 和 nextElementSibling 一样,请记住,名称中带有“element”的属性总是返回Elementor null。没有的属性可以返回任何其他类型的节点。
console.log(document.body.parentNode, "is body's parent node"); // returns <html>
console.log(document.body.parentElement, "is body's parent element"); // returns <html>
var html = document.body.parentElement;
console.log(html.parentNode, "is html's parent node"); // returns document
console.log(html.parentElement, "is html's parent element"); // returns null
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
125609 次 |
| 最近记录: |