In Firefox,CodeThe line feed in is also considered as a node of Type 1 and name # text. But we are usually not interested in line breaks in the code. In this case, we can use nodetype to filter out unnecessary elements. You can use the following JS Code.
For (VAR I = 0; I <nodes. length; I ++ ){
If (nodes [I]. nodetype = 1) {// code, executed only when the node is an element node}
}
Traverse all element nodes of the document
Next we will look at a piece of js code using nodetype. This function traverses all element nodes in the document and counts the total number of element nodes. Of course, we can modify the code to perform any operation on these element nodes. The javascript code is as follows:
<SCRIPT type = "text/JavaScript">
Function countelements (node ){
VaR Total = 0;
If (node. nodetype = 1) {total + = 1 ;}
VaR children = node. childnodes;
For (VAR I = 0; I <children. length; I ++ ){
Total + = countelements (Children [I]);
}
Return total;
}
Function Test2 (){
VaR elementscount = countelements (document );
Alert (elementscount );
}
</SCRIPT>