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

  • 相关阅读:
    LINUX安装 RPM与YUM
    ln s 软链接知识总结
    JQuery EasyUI 之 combobox plugin
    域名的DNS解析指南
    Asp.NET + OWC 输出Chart(图表)
    打败 IE 的葵花宝典:CSS Bug Table
    Route命令使用详解
    JQuery EasyUI 之 validatebox plugin
    JQuery easyUI 之 datebox plugin
    [转]如何安全的存储密码
  • 原文地址:https://www.cnblogs.com/wpfworld/p/2083131.html
Copyright © 2011-2022 走看看