Application of Dom information nodetype

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.