A basic Dom example

Source: Internet
Author: User
Introduction to JS application Dom [1]
DOM object attributes
Dom provides a set of attributes for navigation, accessing, and updating document content, including read-only and read-write attributes.

The following table lists read-only attributes:
DOM object property return value
Firstchild returns an object, indicating the first child node ).
Lastchild returns an object, indicating the last child node ).
Nextsibling returns an object, indicating the next adjacent sibling node.
Nodename returns the HTML Tag corresponding to the node. For example, script. Corresponding to the text item node, # text is returned.
Nodetype: Type of the returned node,
1 indicates that this node is a tag ),
2 indicates attribute ),
3 indicates a text item.
Parentnode returns an object, indicating the parent node of the current node ).
Previussibling returns an object, indicating the previous adjacent sibling node.
Specified returns a Boolean variable, indicating whether attribute is set ).
The following table lists the attributes of the read/write type:
DOM object property return value
Data returns a string that represents the value of the text item node. If it is a node of another type, undefined is returned.
Nodevalue returns a string that represents the value of the text item node. If it is a node of another type, null is returned.
The following table lists the related attributes in the DOM:

DOM object property return value
Attributes indicates the attribute set of a node and is accessed by ID, for example, attributes. Id.
Childnodes indicates the child node set of the node, which is accessed through array indexes, for example, childnodes [2].

<HTML>
<Head>
<Title> simple Dom demo </title>
<Style> type = "text/CSS">
<! --
Form. TB {display: inline ;}
. Twidth {width: 100%}
. Include {font-size: 75%; font-family: verdana, Arial, Helvetica ;}
. Includebig {font-family: verdana, Arial, Helvetica ;}
. Includebig A: link {color: Blue ;}
. Includebig A: visited {color: Purple ;}
. Include a: link {color: Blue ;}
. Include a: visited {color: Purple ;}
. Submitter {font-size: 75%; font-family: verdana, Arial, Helvetica ;}
Pre. Code {color: #660099; margin-left: 5%}
Address {text-align: right}
Body {Background: # ffffff; margin-left: 5%; margin-Right: 5%}
-->
</Style>
</Head>
<Body id = "bodynode">
<P id = "p1node"> This is paragraph 1. </P>
This is the document body
<P id = "p2node"> </P>
<P id = "p3node"> </P>
<Script language = "JavaScript">
Alert (
"Bodynode. firstchild. nodename =" + bodynode. firstchild. nodename + "\ n" +
"Bodynode. firstchild. Data =" + bodynode. firstchild. Data + "\ n" +
"Bodynode. childnodes [0]. nodename =" + bodynode. childnodes [0]. nodename + "\ n" +
"Bodynode. childnodes [1]. nodename =" + bodynode. childnodes [1]. nodename + "\ n" +
"Bodynode. childnodes [1]. Data =" + bodynode. childnodes [1]. Data + "\ n" +
"Bodynode. childnodes [3]. nodename =" + bodynode. childnodes [3]. nodename + "\ n" +
"Bodynode. childnodes [4]. nodename =" + bodynode. childnodes [4]. nodename + "\ n" +
"P1node. nextsibling. nodename =" + p1node. nextsibling. nodename + "\ n" +
"P1node. nextsibling. nextsibling. nodename =" + p1node. nextsibling. nextsibling. nodename + "\ n" +
"P1node. nextsibling. nodename =" + p1node. nextsibling. nodename + "\ n" +
"P3node. previussibling. childnodes [0]. nodename =" +
P3node. previussibling. childnodes [0]. nodename + "\ n" +
"P1node. parentnode. nodename =" + p1node. parentnode. nodename + "\ n" +
"P2node. parentnode. nodename =" + p2node. parentnode. nodename + "\ n" +
"P3node. parentnode. nodename =" + p3node. parentnode. nodename + "\ n" +
"Bodynode. firstchild. firstchild. parentnode. parentnode. nodename =" +
Bodynode. firstchild. firstchild. parentnode. parentnode. nodename + "\ n"
);
</SCRIPT>
</Body>
</Html>

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.