代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>统计element节点</title> <script language="javascript"> var elementname=""; function counttotalelement(node) { ///attribute nodetype值为2,表示节点属性 ///comment nodetype值为8,表示注释文本 ///document nodetype值为9,表示document ///documentfragment nodetype值为11,表示document片段 ///element nodetype值为1,表示元素节点 ///text nodetype值为3,表示文本节点 var total=0; if(node.nodetype==1) //1代表节点的类型为element { total++; elementname=elementname+node.tagname+"\r\n"; } var childrens=node.childnodes; for(var i=0;i<childrens.length;i++) { total+=counttotalelement(childrens[i]); } return total; } </script> </head> <body> <h1>测试</h1> <table width="100" border="2" cellpadding="0" cellspacing="0"> <tr><td> <form name="form1" action="" method="post"> <input type="text" name="ipput1" value="测试"><br /> <input type="password" name="password" value=""> </form> </td></tr> </table> <a href="javascript:void(0)" onclick="alert('标记总数'+counttotalelement(document)+'\r\n 全部标记如下:\r\n'+elementname);">开始测试</a> </body> </html>
其实,通过递归调用也可以实现 想百度蜘蛛爬虫一样的效果!这个值得一试,或许可以通过这个方法,写一个sitemap生成器!
更多js获取html页面节点方法(递归方式)。