• 递归获取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

  • 相关阅读:
    GitLab 介绍
    git 标签
    git 分支
    git 仓库 撤销提交 git reset and 查看本地历史操作 git reflog
    git 仓库 回退功能 git checkout
    python 并发编程 多进程 练习题
    git 命令 查看历史提交 git log
    git 命令 git diff 查看 Git 区域文件的具体改动
    POJ 2608
    POJ 2610
  • 原文地址:https://www.cnblogs.com/wpfworld/p/2083131.html
Copyright © 2020-2023  润新知