JS: childnodes, firstchild, lastchild

Source: Internet
Author: User

In JavaScript, you can use the childnodes attribute to return an array containing all the subnodes of a given element node, as shown in figureCodeAs shown in:

Flat view printing?
01 <Body>
02 <Ul ID="Action">
03 <Li Title="First text"> First </Li>
04 <Li Title="Second paragraph"> Second </Li>
05 </Ul>
06 <Script Type="Text/JavaScript">
07 VaR attr_p = Document. getelementbyid ("action ");
08 Alert (attr_p.childnodes [1]. childnodes [0]. nodevalue );
09 </Script>
10 </Body>

You can use... Childnodes [1]. childnodes [0]. nodevalue... Childnodes [1]. firstchild. nodevalue can also find the first Li text node,
Conclusion childnodes [0] is equivalent to firstchild. whenever and wherever it is necessary to access the first element of the childnodes [] array, we can write it as firstchild, dom also provides a corresponding lastchild attribute.
Note that you can use the nodetype attribute to determine the node type for the FF space node until the element node is found.

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.