JavaScript DOM node tree

Source: Internet
Author: User

1.

For example:

<title> node Tree <title/>

<body>

<p> Student Information <p/>

<ul id = "UL1" >

<li> name <li/>

<li id= "Sex" > Sex <li/>

<li> Age <li/>

<ul/>

<body/>

The child nodes of HTML are body and head while body and head are two sibling nodes, and the parent node is HTML. The child node of head is the title.

2. Accessing the specified element (method)

getElementById (); Note: Gets the element with the specified ID and returns an object. Getelementsbyname (); Note: Gets the element with the specified name, and returns a collection. Geteilementsbytagname (); Note: The element that gets the specified label returns a collection. Getelementsbyclassname (); Note: Gets the element of the specified class and returns a collection.

For example:

Get the LI tag with the ID of sex:

var v = document.getElementById ("Sex");

Get all the LI tags

var v = document.geteliementsbytagname ("Li");

3. Accessing the specified element (attribute)

parentnode; Note: Gets the parent tag of the element, returning an object because each tag has only one parent tag. Chidnodes; Note: Gets the child tag of the specified element, the space is also a child tag, and returns a collection. FirstChild; Note: Gets the first child node and returns an object. LastChild; Note: Gets the last child node that returns an object. previoussibling; Note: Gets the previous sibling node and returns an object. nextSibling; note: After a sibling node is obtained, an object is returned.

For example:

Get Li's parent tag ul

var v = document.getElementById ("Sex"). parentnode;

Get UL's sub Mark Li

var v = document.getElementById ("Ul1"). ChildNodes;

JavaScript DOM node tree

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.