The difference between document.all and Document.getelementsbyname
Source: Internet
Author: User
When the controls on the page have the same name and multiple, it is necessary to judge the length from the strict point of the program, and the length and length are two methods of reference.
oele= DOCUMENT.ALL.AAA//There is a AAA object here, but we do not know how long it is now, so there is no way to operate on it. Therefore, we have to do the process of judging length first.
if (oele.length) {}else{};
In both cases, the contents of the curly braces are written differently:
if (oele.length) {
for (var i = 0; i<oele.length;i++) {
Oele[i].value..
}
}
else{
Oele.value..
};
But is it too complicated to write? And when the curly braces inside the code is more, we have to write two times code, dizzy first ~
Okay there
Document.getelementsbyname ()
This method. It is the same with a number of processing, we can use:
Oele = Document.getelementsbyname (' aaa ')
To reference
When the Oele only 1, then is oele[0], there are multiple times, with the subscript method Oele[i] Loop to obtain, is not very simple?
It is worth mentioning that it is equally valid for name and ID. (It can only be applied to the Document object)
<div Id=radiodiv language=javascript >
<input name=radio1 value=1 type= "Radio" >
<input name=radio1 value=0 type= "Radio" checked>
</div>
Document.getelementsbyname ("Radio1"). Item (0). Value result is 1
There is, however, another way to apply a wider range of objects:
Radiodiv.getelementsbytagname ("Input"). Item (0). Value result is 1
Like I know a <div id= ' aaa ' ><input name=input1 value=1 type=radio ><input name=input1 value=2 type=radio> ......</div> I want to take all the input in the DIV, so I can write it:
Aaa.getelementsbytagname (' INPUT ')
This can be effective and other div (for example, there is a div called BBB, which is the same input) phase difference.
Corresponding to the getElementsByTagName,
There is also a document.body.all.tags ()
The object to use this method is much smaller than the getElementsByTagName. But more than Getelementsbyname.
We're going to have to mention getElementById here.
It is also only the document object to use, and returns the first element of the array, hehe, its method name is getelement rather than getelements
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.