Js gets the HTML page node method of recursive mode of

  • 2020-03-30 00:52:38
  • OfStack

I haven't done a recursive call in a long time. After reading, suddenly woke up!


<!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> statistical Element node </title>
   <script language="javascript">
         var  elementName="";
   function countTotalElement(node)
   {
       /// Attribute  The nodeType value is 2 and represents the node attribute
    /// Comment      The nodeType value is 8, which represents the comment text
    /// Document    The nodeType value is 9, meaning Document
    /// DocumentFragment    The nodeType value is 11, which represents the Document fragment
    /// Element                      The nodeType value is 1 and represents the element node
    /// Text                            The nodeType value is 3, representing the text node
       var total=0;
    if(node.nodeType==1) //1 represents that the node is of type Element
    {
       total++;
    elementName=elementName+node.tagName+"rn";

    }

    var childrens=node.childNodes;
    for(var i=0;i<childrens.length;i++)
    {
        total+=countTotalElement(childrens[i]);
    } 
    return total;
   }
   </script>
</head>
<body>
     <h1> test </h1>
     <table width="100" border="2" cellpadding="0" cellspacing="0">
         <tr><td>
         <form name="form1" action="" method="post">
               <input type="text" name="ipput1" value=" test "><br />
               <input type="password" name="password" value="">
         </form>
         </td></tr>
     </table>
     <a href="javascript:void(0)" onClick="alert(' The total number of tag '+countTotalElement(document)+'rn  All marked as follows :rn'+elementName);"> To begin testing </a>
</body>
</html>

In fact, through recursive call can also achieve the same effect as baidu spider crawler! It's worth a try, maybe by using this method, write a sitemap generator!


Related articles: