Page 1/2 of detailed Javascript DOM Study Notes

Source: Internet
Author: User
Tags javascript array

I. Dom Basics
1. node level
Document-the top-level node. All other nodes are attached to it.
Documenttype -- DTD reference (use <! Doctype> syntax). It cannot contain subnodes.
Documentfragment -- other nodes can be saved like a document.
Element -- indicates the content between the start tag and the end tag, for example, <tag> </TAB> or <tag/>. This is the only node type that can contain both features and subnodes.
ATTR -- represents a pair of feature names and feature values. This node type cannot contain subnodes.
Text indicates the plain text contained in the XML document between the start tag and the end tag, or in the cdatasection. This node type cannot contain subnodes.
Cdatasection -- <! [CDATA []> Object Representation. This node type can only contain text nodes as subnodes.
Entity -- represents an entity definition in the DTD, for example, <! Entity foo "foo">. This node type cannot contain subnodes.
Entityreference -- represents an entity reference, for example ". This node type cannot contain subnodes.
Processinginstruction -- represents a PI. This node type cannot contain subnodes.
Comment -- represents XML comments. This node cannot contain subnodes.
Notation -- represents the mark defined in the DTD. This is rarely used.

The node interface defines the features and methods of all node types.

Features/methods Type/return type Description
Nodename String The node name. It is defined based on the node type.
Nodevalue String Value of a node. It is defined based on the node type.
Nodetype Number Node type constant value
Ownerdocument Document Document pointing to the node
Firstchild Node Point to the first node in the childnodes list
Lastchild Node Point to the last node in the childnodes list
Childnodes Nodelist List of all child nodes
Previussibling Node Indicates a forward sibling node. If this node is the first sibling node, the value is null.
Nextsibling Node Point to the next sibling node. If this node is the last sibling node, the value is null.
Haschildnodes () Boolean Returns true if childnodes contains one or more nodes.
Attributes Namednodemap Contains ATTR objects that represent the characteristics of an element. It is only used for element nodes.
Appendchild (node) Node Add node to the end of childnodes
Removechild (node) Node Delete a node from childnodes
ReplaceChild (newnode, oldnode) Node Replace oldnode in childnodes with newnode
Insertbefore (newnode, refnode) Node Insert newnodd before refnode in childnodes

In addition to nodes, Dom also defines helper objects that can be used with nodes, but are not part of the DOM document.
Nodelist-node array, which is indexed by numerical value. It is used to represent a subnode with an element.
Namednodemap-a node table that uses both values and names for indexing. It is used to represent element features.

2. Access related nodes
In the following sections, consider the following HTML page< Html >
< Head >
< Title > Dom example </ Title >
</ Head >
< Body >
< P > Hello world! </ P >
< P > Isn' t this exciting? </ P >
< P > You're learning to use the Dom! </ P >
</ Body >
</ Html >

To access the VaR ohtml = document.doc umentelement;
Now the variable ohtml contains an htmlelement object that represents VaR ohead = ohtml. firstchild;
VaR obody = ohtml. lastchild;
You can also use the childnodes feature to do the same job. You only need to treat it as a common JavaScript Array and mark it with square brackets:
VaR ohead = ohtml. childnodes [0];
VaR obody = ohtml. childnodes [1];
Note that the square brackets mark is actually a simple implementation of nodelist in JavaScript. In fact, the method for obtaining subnodes from the childnodes list is to use the item () method:
VaR ohead = ohtml. childnodes. Item (0 );
VaR obody = ohtml. childnodes. Item (1 );
The HTML Dom page defines document. body as a pointer to the <body/> element.
VaR obody = ducument. Body;
With the three variables ohtml, ohead, and obody, you can try to determine the relationship between them:
Alert (ohead. parentnode = ohtml );
Alert (obody. parentnode = ohtml );
Alert (obody. previussibling = ohead );
Alert (bhead. nextsibling = obody );
Alert (ohead. ownerdocument = document );
All of the above are outputs "true ".

3. processing Features
As mentioned above, even if the node interface already has an attributes method and has been inherited by all types of nodes, only the
element node can have features. The attributes attribute of the element node is actually namenodemap, which provides some methods for accessing and processing its content:
getnameditem (name) -- return the node with the nodename attribute value equal to name;
removenameditem (name) -- delete a node whose nodename attribute value is equal to name;
setnameditem (node) -- add a node to the list and index it according to its nodename attribute;
item (POS) -- returns the node at the position POS like nodelist.
Note: all these methods return an ATTR node, not a specific value.

The namednodemap object also has a Length attribute to indicate the number of nodes it contains.
When namednodemap is used to represent a feature, each node is an ATTR node. The nodename attribute is set as the feature name, And the nodevalue attribute is set as the feature value. For example, suppose there is such an element:
<P style = "color: Red" id = "p1"> Hello world! </P>
In addition, assume that the variable op contains a reference pointing to this element. You can access the value of the ID feature as follows:
VaR SID = op. Attributes. getnameditem ("ID"). nodevalue;
Of course, you can also use numeric values to access the ID feature, but this is slightly less intuitive:
VaR SID = op. Attributes. Item (1). nodevalue;
You can also change the ID feature by assigning a new value to the nodevalue attribute:
Op. Attributes. getnameditem ("ID"). nodevalue = "newid ";
The ATTR Node also has a value attribute that is completely equivalent to (and fully synchronized with) the nodevalue attribute, and the name attribute and nodename attribute are synchronized. We can use these attributes to modify or change features at will.
Because this method is cumbersome, Dom defines three element methods to help access features:
Getattribute (name) -- equal to attributes. getnameditem (name). value;
Setattribute (name, newvalue) -- equal to attribute. getnameditem (name). value = newvalue;
Removeattribute (name) -- equal to attribute. removenameditem (name ).

4. Access a specified Node
(1) Getelementsbytagname ()
The core (XML) Dom defines the getelementsbytagname () method to return the nodelist of an element containing all tagnames (tag names) that is equal to a specified value. In the element object, the tagname feature is always the name followed by a smaller sign. For example, the tagname of is "IMG ". Next line Code Return the list of all elements in the document:
VaR oimgs = Document. getelementsbytagname ("IMG ");
After saving all the images in oimgs, you only need to use square brackets or the item () method (getelementsbytagname () to return a nodelist with the same name as childnodes ), you can access these nodes one by one as you access the subnodes:
Alert (oimgs [0]. tagname); // outputs "IMG"
If you only want to obtain all the images in the first section of a page, you can call getelementsbytagname () for the first section element, as shown in the following code:
VaR Ops = Document. getelementbytagname ("p ");
VaR oimgsinp = ops [0]. getelementbytagname ("IMG ");
You can use an asterisk to obtain all elements in the document:
VaR oallelements = Document. getelementsbytagname ("*");
When the parameter is an asterisk, ie6.0 does not return all elements. You must use document. All to replace it.
(2) Getelementsbyname ()
The HTML Dom defines getelementsbyname (), which is used to obtain all elements whose name attribute is equal to the specified value.
(3) Getelementbyid ()
This is the second method defined by HTML Dom. It returns the element whose ID attribute is equal to the specified value. In HTML, the ID feature is unique-this means that no two elements can share the same ID. Undoubtedly, this is the fastest way to get a single specified element from the document tree.
Note: If the given ID matches the name attribute of an element, ie6.0 returns this element. This is a bug and must be very careful.

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.