A complete summary and case of document. all

Source: Internet
Author: User

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.
Copy codeThe Code is as follows:
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>

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.