I.
Document. all is a collection of all elements on the page. For example:
Document. all (0) indicates the first element in the page.
II.
Document. all can judge whether the browser is IE
If (document. all ){
Alert ("is IE! ");
}
III.
You can also set the id attribute (id = aaaa) for an element, and then use document. all. aaaa to call this element.
4.
Case:
Code 1:
Copy codeThe Code is as follows:
<Input name = aaa value = aaa>
<Input id = bbb value = bbb>
<Script language = Jscript>
Alert (document. all. aaa. value) // obtain value based on name
Alert (document. all. bbb. value) // obtain value based on id
</Script>
Code 2:
However, names can often be the same (for example, using checkbox to retrieve users' interests)
Copy codeThe Code is 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:
In theory, the IDs on a page are different from each other. If different tags have the same id
Document. all. id will fail, just like this:
Copy codeThe Code is 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 long, or the id is automatically generated by the Program), or
A program written by a beginner in javascript may have two tags with the same id.
To avoid errors during programming, I recommend the following method:
Copy codeThe Code is 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>