zoukankan      html  css  js  c++  java
  • 递归获取html页面节点

    今天在联系JavaScript的时候,找到了这样一段代码示例,

    很久没有操作过递归调用了。看完之后,蓦然惊醒啊!

    <!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生成器!s

  • 相关阅读:
    【转】iOS深入学习(Block全面分析)
    iOS—请求Web Service
    iOS设计模式——MVC
    iOS基础知识
    iOS学习——常用博客
    【转】使用segue页面间传递数据
    【转】storyboard之 prepareForSegue:sender:
    【转】NSDictionary和NSMutableDictionary用法详解
    配置.pch文件
    MKNetworkKit下载图片并显示在UIImageView上
  • 原文地址:https://www.cnblogs.com/wpfworld/p/2083131.html
Copyright © 2011-2022 走看看