JS's document.all function USES the example

  • 2020-03-30 01:08:13
  • OfStack

one
Document.all is a collection of all the elements in a page. Such as:
Document.all (0) represents the first element in the page
two
Document.all can tell if the browser is IE
If (document. All) {
Alert (" is Internet explorer!" );
}
3.
You can also do this by setting the id attribute (id=aaaa) to an element, and then calling that element with document.all.aaaa
Four.
Case study:

Code 1:
 
<input name=aaa value=aaa> 
<input id=bbb value=bbb> 
<script language=Jscript> 
alert(document.all.aaa.value) //Take value based on name
alert(document.all.bbb.value) //Take value by id
</script> 

Code 2:
But often the name can be the same (e.g.
 
<input name=aaa value=a1> 
<input name=aaa value=a2> 
<input id=bbb value=bbb> 
<script language=Jscript> 
alert(document.all.aaa(0).value) //According to a1
alert(document.all.aaa(1).value) //According to a2
alert(document.all.bbb(0).value) //This line of code will fail
</script> 

Code 3:
In theory, 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:
 
<input id=aaa value=a1> 
<input id=aaa value=a2> 
<script language=Jscript> 
alert(document.all.aaa.value) //Undefined instead of a1 or a2
</script> 

Code 4:
For a complex page (the code is 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 not to make mistakes when programming, I recommend this:
 
<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> 

Related articles: