Differences between document. getElementByid, document. all, and document. layers in js

Source: Internet
Author: User

Document. all is a proprietary attribute of IE 4.0 and later versions. It is a group of all objects in the current document, including not only object objects visible on the page, but also invisible objects, for example, html annotations. In the document. all array, elements are listed in parallel according to the order in which they appear in the document. Therefore, you can use a digital index to reference any element. However, it is commonly used to reference a specific object with the Object id, such as document. all ["element.
Document. layers is a proprietary attribute of Netscape 4.x. It is an array representing all elements located by storage such as <div> <layer>. It is usually referenced by the id attribute of the <div> or <layer> object, but it does not contain other elements.
In fact, these two attributes are not comparable. You may often see a historical reason when they come out at the same time. When the fourth generation of browsers appeared, the standards were quite messy. Netscape and Microsoft released their Navigator 4. x and IE 4.0, the huge difference between the two browsers, also makes developers face a nightmare of cross-browser web page compatibility. Document. layer and document. all are two of the most significant signs. To determine which browsers the viewer uses, we usually judge whether document. layers and document. all exist.
The new Unified Standard Uses document. getElementById and other methods to reference DOM objects, and Netscape 6.0 later abandoned the layers feature, although IE continues to retain the document. all, but this does not end up being part of the DOM standard. We hope that document. layers and document. all can be used as early as possible, so that the standards will become deeply rooted in the hearts of the people!
Sample Code: Copy codeThe Code is as follows: <script language = "JavaScript">
Function showtime ()
{
Var GetTime = newDate (); // get the current date
Var Years = GetTime. getYear (); // The Year of the current date.
Var Months = GetTime. getMongh (); // The month of the current date.
Var Days = GetTime. getDate (); // get the day of the current date
Var Hours = GetTime. getHours (); // the hour of the current date.
Var Minutes = GetTime. getMinutes (); // The minute of the current date.
Var Seconds = GetTime. getSeconds (); // the second of the current date.
Var Dn = 'am ';
If (Hours <= 9) // when the hour is less than 9, add 0 to the front,
{
Hours = '0' + Hours;
}
If (Hours = 0 | Hours> 12)
{
Dn = 'pm '; // when the hour is greater than 12 or 0, it is set to PM (afternoon)
}
If (Minutes <= 9)
{
Minutes = '0' + Minutes;
}
If (Seconds <= 9)
{
Seconds = '0' + Seconds; // when the number of Seconds is less than 9, add 0 to the front,
}
NowTime = Years + 'Year' + Months + 'month' + Days + 'day' + Hours + ':' + Minutes + ':' + Seconds + ''+ Dn; // string of the current date
If (document. layers) {// display the string of the current date in different browsers.
Document.layers.liveclock.doc ument. write (NowTime)
Document.layers.liveclock.doc ument. close ()
}
Else if (document. all)
Liveclock. innerHTML = NowTime
SetTimeout ('showtime () ', 1000) // run the showtime function once per second,
}

Document. all is a set of all elements under document under IE.
You can see the number of elements in the document through document. all. length.
The index parameter after document. all (index) is a set method.
If index is string, you can obtain the element with id or name as index under document. If there is only one element, this element is returned. If there are multiple element IDs or names as index, if the set does not exist, null is returned.
This is not good because it is easy to make mistakes when you do not know how many IDs or names are index elements.
If the index is a number, the document sequence of the index Element. Element in the 0-based document can be obtained through Element. sourceIndex.
Document. getElementById (name) returns only the first element with id or name as name. Therefore, if it is not null, it is an element and does not return a set. This greatly reduces the possibility of errors.
If you need to return a set of elements whose id or name is name, you need to use document. getElementsByName (name) to obtain
Document. getElementById
Returns the first object with the same value as the specified value. If the ID belongs to a set, the getElementById method returns the first object in the set.
Document. all
If the ID belongs to a set, document. all returns a set. Only Internet Explorer is supported.
Therefore, to use IE, document. all (index) must be unique to take effect.
Therefore, if you want to use the Javascript script that you write in most browsers, you need to use the [public standard] standard Javascript

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.