js| Web page
getElementById getelementsbyname getelementsbytagname about
getElementById, getelementsbyname, getelementsbytagname
The latter two are getting set, Byid just getting a single object
The use of getElementById
As an example:
<a id= "Link1" name= "Link1" href=http://www.webjx.com> Web Teaching Network </a>
Reference methods within the same page:
1. Use ID:
Link1.href, the return value is http://www.webjx.com
2. Use name:
Document.all.link1.href, the return value is http://www.webjx.com
3, the use of Sourseindex:
document.all (4). href/note, the front also has HTML, head, title and body, so it is 4
4, use the link collection:
Document.anchors (0). href
All collections have 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 are the same set of uses, just one is all, which can include all the tags on the page, and anchors only includes links.
5, getElementById:
document.getElementById ("Link1"). href
6, Getelementsbyname:
Document.getelementsbyname ("Link1") [0].href//This is also a collection, a collection of all tags with the name equal to the parameters of the method
7, getElementsByTagName:
document.getElementsByTagName ("a") [0].href//This is also a collection that is a collection of all tags with a tag name equal to the parameters of the method
8, Tags set:
Document.all.tags ("A") [0].href
As with Method 7, you get a collection by tag name
Besides:
Event.screlement can obtain the reference of the marker of the triggering time;
Document.elementfrompoint (x,y) can obtain references to elements at the x and y coordinates;
Document.body.componentFromPoint (event.clientx,event.clienty) Gets a reference to the element at which the mouse is located;
You can also refer to the element's parent-child node and sibling-node relationship, such as nextsibling (the latter node of the current node), previoussibling (the previous node of the current node), childnodes, children, FirstChild, LastChild, parentelement, etc. are all references to parent-child nodes and sibling nodes;
This is a common reference method on the same page, and it is also involved in different pages of
Getelementsbyname returns a collection of all the elements named with the specified value
"Gets a collection of objects based on the value of the NAME label property. ”
The collection is much looser than the array, the type of each subkey in the collection can be different, and the collection simply puts some elements together as a class, and the array is much stricter, and each subkey is a unified type. Document.getelementsbyname, document.getElementsByTagName, document.formName.elements the result of such a method is a collection.
Cases:
<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:" + ele.length);
}
</script><body>
<H2 id= "BBS" > get the element specified by the file <form>
<input type= "button" value= "Get title Tag" >
<input type= "button" name= "Happy" value= "click" ><input type= "button" name= "Happy" value= "click" >< Input type= "button" name= "Happy" value= "click" ><input type= "button" name= "Happy" value= "click" ><input typ E= "button" name= "Happy" value= "click" >
</form>
</body>
Document.getelementsbyname () This method. It is the same with a plurality of processing, we can use:
Temp = Document.getelementsbyname (' happy ') to reference
When only 1 of the temp, then is temp[0], there are multiple times, with the subscript method Temp[i] loop to get
There are also exceptions:
When Getelementsbyname ("test") in IE returns an array of Id=test object, Firefox returns an array of Object Name= test.
According to the specifications of the name=, you should return an array of object for test.
Firefox is the same as getElementById in IE: Gets a reference to the first object with the ID label property as the specified value.
Note that Getelementsbyname has s in it.
document.getElementById () can control the tag of an ID
Document.getelementsbyname () returns a collection of elements with the same name property, not one, with the note "s".
document.getElementsByTagName () returns a collection of elements of the same TAG.
The same name can have more than one element, so use Document.getelementsbyname ("Thename")
He return a collection, quoting the time to be named index
var test = document.getelementsbyname (' Testbutton ') [0];
ID that, is the only
It should also be noted that for a similar absence of the Name property, the Name property Document.getelementsbyname () is invalidated, and of course TD can set the ID attribute and then use Document.getelementsbyid ("Dde_ Noday "); Call