one.
document.all is a collection of all elements in the page. For example:
document.all(0) represents the first element in the page
two.
document.all can determine whether the browser is IE
if(document.all){
alert("is IE!");
}
three.
You can also set the id attribute (id=aaaa) to an element and then call the element using document.all.aaaa
Four.
Case:
Code 1:
Copy the code code as follows:
<input name=aaa value=aaa>
<input id=bbb value=bbb>
<script language=Jscript>
alert(document.all.aaa.value) //Get value based on name
alert(document.all.bbb.value) //Get value based on id
</script>
Code 2:
But often the names can be the same (for example, when using checkbox to retrieve multiple hobbies of the user)
Copy the code code as follows:
<input name=aaa value=a1>
<input name=aaa value=a2>
<input id=bbb value=bbb>
<script language=Jscript>
alert(document.all.aaa(0).value) //Display a1
alert(document.all.aaa(1).value) //Display a2
alert(document.all.bbb(0).value) //This line of code will fail
</script>
Code 3:
Theoretically, the IDs in a page are different from each other. If different tags appear, they have the same ID.
document.all.id will fail, like this:
Copy the code code as follows:
<input id=aaa value=a1>
<input id=aaa value=a2>
<script language=Jscript>
alert(document.all.aaa.value) //Display undefined instead of a1 or a2
</script>
Code 4:
For a complex page (the code is very long, or the ID is automatically generated by the program), or a
For programs written by JavaScript beginners, it is very likely that two tags will have the same ID.
In order to avoid errors when programming, I recommend writing like this:
Copy the code code as follows:
<input id=aaa value=aaa1>
<input id=aaa value=aaa2>
<input name=bbb value=bbb>
<input name=bbb value=bbb2>
<input id=ccc value=ccc>
<input name=ddd value=ddd>
<script language=Jscript>
alert(document.all("aaa",0).value)
alert(document.all("aaa",1).value)
alert(document.all("bbb",0).value)
alert(document.all("bbb",1).value)
alert(document.all("ccc",0).value)
alert(document.all("ddd",0).value)
</script>