for (let i = 0; i < document.body.childNodes.length; i++) {
alert( document.body.childNodes[i] ); // Text, DIV, Text, UL, ..., SCRIPT
}
elem.childNodes[0] === elem.firstChild
elem.childNodes[elem.childNodes.length - 1] === elem.lastChild
for (let node of document.body.childNodes) {
alert(node); // 显示集合中的所有节点
}
alert(document.body.childNodes.filter); // undefined(这里没有 filter 方法!)
alert( Array.from(document.body.childNodes).filter ); // function
//<body> 的父节点是 <html>
alert( document.body.parentNode === document.documentElement ); // true
// <head> 的后一个是 <body>
alert( document.head.nextSibling ); // HTMLBodyElement
// <body> 的前一个是 <head>
alert( document.body.previousSibling ); // HTMLHeadElement
alert( document.documentElement.parentNode ); // document
alert( document.documentElement.parentElement ); // null
<html>
<body>
<div>Begin</div>
<ul>
<li>Information</li>
</ul>
<div>End</div>
<script>
for (let elem of document.body.children) {
alert(elem); // DIV, UL, DIV, SCRIPT
}
</script>
...
</body>
</html>
给定一个 DOM 节点,我们可以使用导航(navigation)属性访问其直接的邻居。