Get child element nodes (children, childNodes), childrenchildnodes

Source: Internet
Author: User

Get child element nodes (children, childNodes), childrenchildnodes

Methods for Retrieving Element nodes in js that have DOM, such as getElementsByTagName .. There is another way to obtain child elements: ChildNodes

However, ChidNodes is "not normal" in the advanced browser except (IE6-8), because it not only obtains the element node but also obtains the text node, this is the legendary buy one get one free? However, the one-to-one activity can be used with nodeType when we don't want it. The Code is as follows:

Of course, this judgment is for advanced browsers. IE6-8 is not to judge nodeType, because it was originally obtained only element nodes, no text nodes.

In addition to the childNodes method to obtain sub-elements, there is also a method called children, this is not to judge the compatibility, IE6-8, other advanced browsers support. The usage is as follows:

Let's talk about the methods for obtaining these two nodes: firstChild (firstElementChild) [first child element], lastChild (lastElementChild) [last child element], nextSibling (nextElementSibling) [Next element of the same level], previousSibling (previousElementSibling) [previous element of the same level], parentNode [parent node]

Among them, the brackets are in the advanced browser to get the Element Node Method, inside the brackets (IE6-8) and inside the brackets (advanced browser) is equivalent to solving a compatibility problem. I will not give an example.

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.