Getelementbyid getelementsbyname getelementsbytagname
Getelementbyid obtains the node through the ID attribute of the node. This object contains attributes such as nodename, nodetype, parentnode, and childnodes.
Nodename indicates the node name. If it is an element node, the return value of nodename is the tag name, and the tag name is always in upper case. If it is an attribute node, the return value of nodename is the name of the attribute. If it is a text node, the return value of nodename is always the # Document identifier.
Nodetype indicates the node type. There are many return values for this attribute. The important node types include: 1 indicating the element type, 2 indicating the attribute, 3 indicating the text, 8 indicating the comment, and 9 indicating the document.
The parentnode attribute returns the reference pointer of the parent node.
The childnodes attribute indicates all child elements contained in the current element. Therefore, this attribute returns an array containing the sequence of all child elements.
Getelementsbytagname () obtains a node set based on the tag of the specified name. You can use document. getelementsbytagname ("*") to obtain all element nodes in the document. However, this method is rarely used. At the same time, ie6.0 and its support for browsers of the next version is not very good. For IE browser, you can obtain all element nodes in the document through document. All.
The last two are the collection, and the byid is only a single object.
Getelementbyid usage
For example:
<A id = "link1" name = "link1" href = http://homepage.yesky.com> Web taoba </a>
Reference Method on the same page:
1. Use ID:
Link1.href, return a http://homepage.yesky.com
2. Use name:
Document. All. link1.href, return a http://homepage.yesky.com
3. Use sourseindex:
Document. All (4). href // note that there are HTML, Head, title, and body in front, so it is 4
4. Use a link set:
Document. Anchors (0). href
// All sets include all, anchors, applets, areas, attributes, behaviorurns, bookmarks, boundelements, cells, childnodes, children, controlrange, elements, embeds, filters, forms, frames, images, imports, links, mimetypes, options, plugins, rows, rules, scripts, stylesheets, tbodies, textrectangle, please refer to the msdn introduction.
In fact, method 3 and method 4 use the same set, but one is all, which can include all the tags on the page, while anchors only includes links.
5. getelementbyid:
Document. getelementbyid ("link1"). href
6. getelementsbyname:
Document. getelementsbyname ("link1") [0]. href // This is also a set of all tags whose names are equal to the parameters included in the Method
7. getelementsbytagname:
Document. getelementsbytagname ("A") [0]. href // This is also a set of all tags whose names are equal to the parameters included in the Method
8. tags set:
Document. All. Tags ("A") [0]. href
// Like Method 7, a set is obtained by Tag name.
In addition:
Event. screlement can be referenced by the trigger time tag;
Document. elementfrompoint (x, y) can be used to obtain references to elements in the X and Y coordinates;
Document. Body. componentfrompoint (event. clientx, event. clienty) can be used to obtain the reference of the element where the mouse is located;
It can also be referenced through the relationship between parent and child nodes and sibling nodes of the element, such as nextsibling (the last node of the current node) and previussibling (the former node of the current node), childnodes, children, firstchild, lastchild, and parentelement are references of parent and child nodes and sibling nodes.
The above is a common reference method on the same page, and also involves
Getelementsbyname returns a set of all elements whose names are specified values.
"Obtains the object set based on the value of the name tag attribute ."
The set is much loose than the array. The types of each item in the set can be different. The set only uses some elements as one type. In contrast, the array is much more rigorous, each subitem is of the same type. document. getelementsbyname, document. getelementsbytagname, document. formname. all the results obtained by methods such as elements are collections.
Example:
<HTML>
<Head>
<Title> fish </title>
<Script language = "JavaScript">
Function get (){
VaR xx = Document. getelementbyid ("BBS ")
Alert ("tag name:" + XX. tagname );
}
Function getelementname (){
VaR ele = Document. getelementsbyname ("happy ");
Alert ("Number of happy users without element:" + ELE. Length );
}
</SCRIPT> <Body>
<H2 id = "BBS"> get the specified element of the file </H2>
<HR>
<Form>
<Input type = "button" onclick = "Get ()" value = "Get Title Tag">
<Input type = "button" name = "happy" onclick = "getelementname () "value =" click "> <input type =" button "name =" happy "onclick =" getelementname () "value =" click "> <input type =" button "name =" happy "onclick =" getelementname () "value =" click "> <input type =" button "name =" happy "onclick =" getelementname () "value =" click "> <input type =" button "name =" happy "onclick =" getelementname () "value =" click ">
</Form>
</Body>
</Html>
Document. getelementsbyname () method. It processes one or more objects in the same way. We can use:
Temp = Document. getelementsbyname ('Happy ') to reference
When there is only one temp, It is Temp [0]. When there are multiple temp, we can use the subscript method temp [I] to obtain them cyclically.
There are also exceptions:
In IE, when getelementsbyname ("test") is returned, an array of objects with ID = test is returned, while Firefox returns an array of objects with name = test.
According to W3C rules, the returned array is the object name = test.
Getelementbyid in Firefox and IE are the same: Get the reference of the first object whose ID label attribute is the specified value.
Note that the getelementsbyname has s in it.
Document. getelementbyid () can control the tag of an ID
Document. getelementsbyname () returns a set of elements with the same name attribute, rather than a set of elements. Note that "S" exists ".
Document. getelementsbytagname () returns a set of elements with the same tag.
The same name can have multiple elements, so use document. getelementsbyname ("thename ")
He returns a collection, which must be named index when referenced.
VaR test = Document. getelementsbyname ('testclick') [0];
ID, which is unique
It should also be noted that there is no name attribute for the like, and its name attribute is a pseudo attribute document. getelementsbyname () will expire. Of course, you can set the ID attribute for TD, and then use document. getelementsbyid ("dde_noday"); call
Createelement () creates a new node based on the specified element name.
Usage: var E = Document. createelement ("element ");
The new node created using the createelement () method is not automatically added to the document, because the new node does not have the nodeparent attribute and is only valid in the JavaScript context. To add this node to the document, you also need to use the appendchild (), insertbefore (), or replaceChild () methods.
var p = document.createElement("p");var h1 = document.createElement("h1");var txt = document.createTextNode("Hello World");p.appendChild(txt);h1.appendChild(p);document.body.appendChild(h1);
Getelementbyid getelementsbyname getelementsbytagname
Getelementbyid obtains the node through the ID attribute of the node. This object contains attributes such as nodename, nodetype, parentnode, and childnodes.
Nodename indicates the node name. If it is an element node, the return value of nodename is the tag name, and the tag name is always in upper case. If it is an attribute node, the return value of nodename is the name of the attribute. If it is a text node, the return value of nodename is always the # Document identifier.
Nodetype indicates the node type. There are many return values for this attribute. The important node types include: 1 indicating the element type, 2 indicating the attribute, 3 indicating the text, 8 indicating the comment, and 9 indicating the document.
The parentnode attribute returns the reference pointer of the parent node.
The childnodes attribute indicates all child elements contained in the current element. Therefore, this attribute returns an array containing the sequence of all child elements.
Getelementsbytagname () obtains a node set based on the tag of the specified name. You can use document. getelementsbytagname ("*") to obtain all element nodes in the document. However, this method is rarely used. At the same time, ie6.0 and its support for browsers of the next version is not very good. For IE browser, you can obtain all element nodes in the document through document. All.
The last two are the collection, and the byid is only a single object.
Getelementbyid usage
For example:
<A id = "link1" name = "link1" href = http://homepage.yesky.com> Web taoba </a>
Reference Method on the same page:
1. Use ID:
Link1.href, return a http://homepage.yesky.com
2. Use name:
Document. All. link1.href, return a http://homepage.yesky.com
3. Use sourseindex:
Document. All (4). href // note that there are HTML, Head, title, and body in front, so it is 4
4. Use a link set:
Document. Anchors (0). href
// All sets include all, anchors, applets, areas, attributes, behaviorurns, bookmarks, boundelements, cells, childnodes, children, controlrange, elements, embeds, filters, forms, frames, images, imports, links, mimetypes, options, plugins, rows, rules, scripts, stylesheets, tbodies, textrectangle, please refer to the msdn introduction.
In fact, method 3 and method 4 use the same set, but one is all, which can include all the tags on the page, while anchors only includes links.
5. getelementbyid:
Document. getelementbyid ("link1"). href
6. getelementsbyname:
Document. getelementsbyname ("link1") [0]. href // This is also a set of all tags whose names are equal to the parameters included in the Method
7. getelementsbytagname:
Document. getelementsbytagname ("A") [0]. href // This is also a set of all tags whose names are equal to the parameters included in the Method
8. tags set:
Document. All. Tags ("A") [0]. href
// Like Method 7, a set is obtained by Tag name.
In addition:
Event. screlement can be referenced by the trigger time tag;
Document. elementfrompoint (x, y) can be used to obtain references to elements in the X and Y coordinates;
Document. Body. componentfrompoint (event. clientx, event. clienty) can be used to obtain the reference of the element where the mouse is located;
It can also be referenced through the relationship between parent and child nodes and sibling nodes of the element, such as nextsibling (the last node of the current node) and previussibling (the former node of the current node), childnodes, children, firstchild, lastchild, and parentelement are references of parent and child nodes and sibling nodes.
The above is a common reference method on the same page, and also involves
Getelementsbyname returns a set of all elements whose names are specified values.
"Obtains the object set based on the value of the name tag attribute ."
The set is much loose than the array. The types of each item in the set can be different. The set only uses some elements as one type. In contrast, the array is much more rigorous, each subitem is of the same type. document. getelementsbyname, document. getelementsbytagname, document. formname. all the results obtained by methods such as elements are collections.
Example:
<HTML>
<Head>
<Title> fish </title>
<Script language = "JavaScript">
Function get (){
VaR xx = Document. getelementbyid ("BBS ")
Alert ("tag name:" + XX. tagname );
}
Function getelementname (){
VaR ele = Document. getelementsbyname ("happy ");
Alert ("Number of happy users without element:" + ELE. Length );
}
</SCRIPT> <Body>
<H2 id = "BBS"> get the specified element of the file </H2>
<HR>
<Form>
<Input type = "button" onclick = "Get ()" value = "Get Title Tag">
<Input type = "button" name = "happy" onclick = "getelementname () "value =" click "> <input type =" button "name =" happy "onclick =" getelementname () "value =" click "> <input type =" button "name =" happy "onclick =" getelementname () "value =" click "> <input type =" button "name =" happy "onclick =" getelementname () "value =" click "> <input type =" button "name =" happy "onclick =" getelementname () "value =" click ">
</Form>
</Body>
</Html>
Document. getelementsbyname () method. It processes one or more objects in the same way. We can use:
Temp = Document. getelementsbyname ('Happy ') to reference
When there is only one temp, It is Temp [0]. When there are multiple temp, we can use the subscript method temp [I] to obtain them cyclically.
There are also exceptions:
In IE, when getelementsbyname ("test") is returned, an array of objects with ID = test is returned, while Firefox returns an array of objects with name = test.
According to W3C rules, the returned array is the object name = test.
Getelementbyid in Firefox and IE are the same: Get the reference of the first object whose ID label attribute is the specified value.
Note that the getelementsbyname has s in it.
Document. getelementbyid () can control the tag of an ID
Document. getelementsbyname () returns a set of elements with the same name attribute, rather than a set of elements. Note that "S" exists ".
Document. getelementsbytagname () returns a set of elements with the same tag.
The same name can have multiple elements, so use document. getelementsbyname ("thename ")
He returns a collection, which must be named index when referenced.
VaR test = Document. getelementsbyname ('testclick') [0];
ID, which is unique
It should also be noted that there is no name attribute for the like, and its name attribute is a pseudo attribute document. getelementsbyname () will expire. Of course, you can set the ID attribute for TD, and then use document. getelementsbyid ("dde_noday"); call
Createelement () creates a new node based on the specified element name.
Usage: var E = Document. createelement ("element ");
The new node created using the createelement () method is not automatically added to the document, because the new node does not have the nodeparent attribute and is only valid in the JavaScript context. To add this node to the document, you also need to use the appendchild (), insertbefore (), or replaceChild () methods.
var p = document.createElement("p");var h1 = document.createElement("h1");var txt = document.createTextNode("Hello World");p.appendChild(txt);h1.appendChild(p);document.body.appendChild(h1);