Differences between childNodes. length and children. length _ javascript skills

Source: Internet
Author: User
The values of childNodes. length and children. length are often different. Because ChildNodes Not only Html Node , AllAttribute,TextAre included in ChildNodes Inside , You can use NodeType To determine the type of node, only when NodeType = 1 Make the element node, 2 Yesattribute node, 3 Is a text node. While Children. length Only count the number of nodes.
This includes calculating the node length. Pay attention to this when moving nodes.
To obtain the text of an element, for example, to obtain P Node text, which is preferred by some InnerHTML And the change is the same. In fact, this is not very good because of the use InnerHTML The browser will think that what you want to insert is a piece of label text, and you just want to insert a piece of plain text, which leads the browser to try to parse your plain text, this will cause unnecessary waste of resources. Therefore, we recommend that you use Element. firstChild. nodeValue, Why? FirstChild What about it? Because ChildNodes [0] It's a text node. You can check it if you don't believe it. NodeType .
Related Article

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.