document.all

1.
document.all is a collection of all elements in the page. For example:     
document.all(0) means the first element in the page
2.
document.all can judge whether the browser is IE 
  if(document.all){ 
    alert("is IE!"); 
  }
3.
You can also pass An element sets the id attribute (id=aaaa), and then calls the element with document.all.aaaa
4.
Case:

Code 1:   
  <input   name=aaa   value=aaa>  
  <input   id=bbb   value=bbb>   
  <script   language=Jscript>  
      alert(document.all.aaa.value) //take value according to name  
      alert(document.all.bbb.value) //take value according to id  
  </script>
  
Code 2: 
      But often the name can be the same (for example: use the checkbox to get multiple hobbies of the user)  
  
  <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: 
      In theory, the ids in a page are different from each other, if different tags 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) //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 
  program written by a javascript beginner, it is very likely that two tags have the same id. 
In order not to make mistakes when programming, I recommend this way of writing: 
  
 
  <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>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327010328&siteId=291194637