代码编辑器:
x
1
<p id="demo">单击按钮获取 body 元素子节点的节点名称</p>
2
<button onclick="myFunction()">点我</button>
3
<script>
4
function myFunction(){
5
var txt="";
6
var c=document.body.childNodes;
7
for (i=0; i<c.length; i++){
8
txt=txt + c[i].nodeName + "<br>";
9
};
10
var x=document.getElementById("demo");
11
x.innerHTML=txt;
12
}
13
</script>
14
<p><strong>注意:</strong> 空格元素是文本,文本是节点</p>
15