How JavaScript counts the number of labels in a page:
This section describes how to count the number of labels in the page, of course, the label can be repeated, although not commonly used, but hope to give you more or less help.
The code is as follows:
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ant Tribe</title><Scripttype= "Text/javascript">varelementname="";functioncounttotalelement (node) {//The /attribute NodeType value is 2, which indicates the node property ///comment NodeType value is 8, indicating the comment text ///document NodeType value is 9, indicating Document ///documentfragment NodeType value is 11, indicating document fragment //The /element NodeType value is 1, which represents the element node //The /text NodeType value is 3, which represents the text node var Total=0; //1 represents the type of node as element if(Node.nodetype==1) { Total++; ElementName=elementname+Node.tagname+"\ r \ n"; } varChildrens=Node.childnodes; for(varI=0; I<Childrens.length;i++) { Total+=counttotalelement (Childrens[i]); } returnTotal ;} Window.onload=function(){ varBT=document.getElementById ("BT"); Bt.onclick=function() {alert ('Total Labels'+counttotalelement (document)+'\ r \ n All tags are as follows: \ n'+elementname); }}</Script></Head><Body><ahref= "javascript:void (0); return false"ID= "BT">Click Test</a></Body></HTML>
The above code implements our requirements, click the button can pop-up box, to count the number of labels on the page. The code is simple, that is, by judging the node type, if the element node is counted, and combined with a for loop through the recursive way to achieve the quantity statistics.
A. Code comment:
1.var elementname= "", declares a variable that stores the name of the label and concatenates the results in a string connection.
2.function counttotalelement (node) {}, this function is the core of the function, the parameter is a node object, in this code is the document node object.
3.var total=0, declares a variable and initializes it to 0, which is used to store the number of labels.
4.if (node.nodetype==1) to determine if it is an element node.
5.total++, plus 1 operations.
6.elementname=elementname+node.tagname+ "\ r \ n", the label name is connected, followed by carriage return line.
7.var Childrens=node.childnodes, gets all the first-level child nodes of the node nodes.
8.for (Var i=0;i<childrens.length;i++), traversing each one-level child node.
9.total+=counttotalelement (childrens), where recursion is used to implement deep traversal, that is, to judge a node first, then iterate over its child nodes, and so on.
10.return Total, which returns the quantity.
two. Related reading:
1.nodeType properties can be found in chapter.
2.tagName properties can be found in the section javascript How to get the TagName label name of an element .
3.childNodes properties can be found in the section
The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=11388
For more information, refer to: http://www.softwhy.com/javascript/
How JavaScript counts the number of labels in a page