var temp=document.getElementsByTagName("div")[0].childNodes;
var temp=document.getElementsByTagName("div")[0].childNodes;
这里的【0】怎么解释?
2016-11-12
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> </head> <body> <div> javascript <p>javascript</p> <div>111</div> <h5>PHP</h5> </div> <div> 哈哈哈 <p>javascript</p> <div>333</div> <h5>PHP</h5> </div> <div> 哼哼 <p>javascript</p> <div>555</div> <h5>PHP</h5> </div> <script type="text/javascript"> var x=document.getElementsByTagName("div")[3].childNodes; for(var i=0;i<x.length;i++) { document.write("第"+(i+1)+"个节点的值是:"+x[i].nodeValue+"<br />"); document.write("第"+(i+1)+"个节点的名称是:"+x[i].nodeName+"<br />"); document.write("第"+(i+1)+"个节点的属性是:"+x[i].nodeType+"<br />"); document.write("<br />"); } </script> </body> </html>
参考以上代码,[3]选择的是第4个div,即 <div>333</div>。
举报