<!DOCTYPE html>
<html>
<body>
<h1>Element 对象</h1>
<h2>normalize() 方法</h2>
<button onclick="addTextNode()">添加文本</button>
<button onclick="normPara()">规范化</button>
<p id="demo">单击一个按钮可向该段落添加文本,单击另一个按钮规范该段落。</p>
<p>上面的段落有 <b><span id="cc">1</span></b> 个子节点。</p>
<script>
function addTextNode() {
const node = document.createTextNode("再次点击。");
const element = document.getElementById("demo");
element.appendChild(node);
countNodes(element);
}
function normPara() {
const element = document.getElementById("demo");
element.normalize();
countNodes(element);
}
function countNodes(element) {
const span = document.getElementById("cc");
span.innerHTML = element.childNodes.length;
}
</script>
</body>
</html>