Differences between childNodes. length and children. length

Source: Internet
Author: User

BecauseChildNodesNot onlyHtmlNode, AllAttribute,TextAre included inChildNodesInside,You can useNodeTypeTo determine the type of node, only whenNodeType = 1Make the element node,2Yesattribute node,3Is a text node. WhileChildren. lengthOnly 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 obtainPNode text, which is preferred by someInnerHTMLAnd the change is the same. In fact, this is not very good because of the useInnerHTMLThe 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 useElement. firstChild. nodeValue,Why?FirstChildWhat about it? BecauseChildNodes [0]It's a text node. You can check it if you don't believe it.NodeType.

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.