专注前端素材!
源代码:
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>素材8网教程(study.sucai8.cn)</title> </head> <body> <p id="demo">单击按钮添加文本,单击另一个按钮标准化元素。</p> <button onclick="addTextNode()">添加一个文本节点</button> <button onclick="normPara()">规格化段落</button> <script> function addTextNode(){ var y=document.createTextNode(" Click again"); var x=document.getElementById("demo"); x.appendChild(y); var z=document.getElementById("cc"); z.innerHTML=x.childNodes.length; } function normPara(){ var x=document.getElementById("demo"); x.normalize(); var z=document.getElementById("cc"); z.innerHTML=x.childNodes.length; } </script> <p>上面的段落有 <span id="cc">1</span>子节点。</p> </body> </html>
运行结果