Differences between children and childnodes

Source: Internet
Author: User
Children and childnodes

1. childnodes attributes, Standard. It returns the child element set of the specified element, including the HTML node, all attributes, and text. You can use nodetype to determine the type of node. Only when nodetype = 1 is the element node, 2 is the attribute node, and 3 is the text node.

Some people mistakenly use () to retrieve the set element. The following table lists the support of various browsers for childnodes (I:

  Internet Explorer 6/7/8/Safari/Chrome/Opera Ie9/Firefox
Childnodes (I) Supported Not Supported

Sometimes the first HTML subnode (non-attribute/text node) of the specified element needs to be obtained, and the firstchild attribute is the most likely to come to mind. If there is a line break or space before the first HTML node in the code, then firstchild does not return what you want. You can use nodetype to determine.

123456 function getFirst(elem){    for(var i=0,e;e=elem.childNodes[i++];){        if(e.nodeType==1)            return e;    }      }

2. Children attributes, Non-standard. It returns the child element set of the specified element. After testing, it only returns HTML nodes, or even does not return text nodes. In addition, it is surprisingly consistent across all browsers. Like childnodes, the set element () is not supported in Firefox. Therefore, if you want to obtain the first HTML node of the specified element, you can use children [0] to replace the above getfirst function. Note that children includes annotation nodes in IE.

Differences between children and childnodes

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.