It's been a long time since I've done a recursive call. After reading it, I suddenly woke up!
Copy the code code as follows:
<!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>Statistics Element Node</title>
<script language="javascript">
var elementName="";
function countTotalElement(node)
{
///Attribute nodeType value is 2, indicating node attributes
///Comment nodeType value is 8, indicating comment text
///Document nodeType value is 9, indicating Document
///DocumentFragment nodeType value is 11, indicating Document fragment
///Element nodeType value is 1, indicating element node
///Text nodeType value is 3, indicating text node
var total=0;
if(node.nodeType==1) //1 represents that the type of node is 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>Test</h1>
<table 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('Total number of tags'+countTotalElement(document)+'/r/n All tags are as follows:/r/n'+elementName);">Start test< /a>
</body>
</html>
In fact, the same effect as Baidu spider crawler can be achieved through recursive calls! This is worth a try. Maybe you can use this method to write a sitemap generator!