<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NodeList和HTMLCollection对比</title>
</head>
<body>
<div id="div">
<p>p1</p>
<h3>h1</h3>
<p>p2</p>
<!-- 注释 -->
<p>p3</p>
<h3>h2</h3>
<p>p4</p>
<h3>h3</h3>
<p>p5</p>
<p>p6</p>
</div>
<script>
/*
nodeList
childNodes
querySelectorAll
HTMLCollection
children
getElementsByTagName
getElementsByClassName
1. nodeList 有 forEach 方法,但是 HTMLCollection 没有forEach
2. HTMLCollection 每次调用时都会动态的去获取, nodeList 中 childNodes 有动态更新,但是querySelectorAll 就不会动态更新
*/
{
let div = document.querySelector("#div");
let nodes = div.querySelectorAll("p");
let collection = div.children;
console.log(nodes);
console.log(collection);
div.innerHTML = "";
console.log(nodes);
console.log(collection);
}
</script>
</body>
</html>