JS document.getElementById, document.all and document.layers to distinguish between the introduction of _javascript skills

Source: Internet
Author: User
Document.all is the proprietary property of IE 4.0 and above, a Louboutin group that represents all the objects in the current document, not only the entity objects visible on the page, but also some objects that are not visible, such as HTML annotations, and so on. In the document.all array, the elements are listed in parallel according to the order in which they appear in the document. So you can use a numeric index to refer to any element. However, it is more commonly used to refer to a particular object with an object ID, such as document.all["element".
Document.layers is a proprietary property of Netscape 4.x, an array representing all the elements that are positioned by storage such as <div><layer>. It is usually referenced by the id attribute of the <div> or <layer> object, but there are no other elements in it.
In fact, these two properties are not comparable, probably you often see them at the same time, this has a historical reason. When the fourth generation of browsers appeared, the standard was quite confusing, Netscape and Microsoft introduced their Navigator 4.x and IE 4.0, the difference between the two browsers, but also the developers face a Web browser compatible with a nightmare. Document.layer and document.all, respectively, are the two most prominent signs, in order to determine what browsers to use, usually with the existence of document.layers and document.all to judge.
The new unified standard uses a series of methods such as document.getElementById to refer to DOM objects, and Netscape has abandoned layers features after 6.0, Although IE continues to retain the document.all, this is not ultimately part of the DOM standard. Hope Document.layers and document.all can soon die, let the standard early popular!
code example:
Copy Code code as follows:

<script language= "JavaScript" >
function Showtime ()
{
var gettime=newdate (); Get Current date
var years=gettime.getyear (); Year to take the current date
var months=gettime.getmongh ();/month of the current date
var days=gettime.getdate ();/day of the current date
var hours=gettime.gethours ()//hour to take the current date
var minutes=gettime.getminutes ();//minutes to take the current date
var seconds=gettime.getseconds ()//seconds to take the current date
var dn= ' AM ';
if (hours<=9)//hour less than 9 o'clock, front plus 0,
{
hours= ' 0 ' +hours;
}
if (hours==0| | HOURS&GT;12)
{
dn= ' PM '; Hours greater than 12 or 0 o'clock, set to PM (PM)
}
if (minutes<=9)
{
minutes= ' 0 ' +minutes;
}
if (seconds<=9)
{
seconds= ' 0 ' +seconds;//seconds less than 9 o'clock, front plus 0,
}
nowtime=years+ ' +months+ ' month ' +days+ ' Day ' +hours+ ': ' +minutes+ ': ' +seconds+ ' +dn; string of current date
if (document.layers) {///under different browsers, the string of the current date is displayed.
Document.layers.liveclock.document.write (Nowtime)
Document.layers.liveclock.document.close ()
}
else if (document.all)
Liveclock.innerhtml=nowtime
SetTimeout (' Showtime () ', 1000)//sec run Showtime function once,
}

Document.all is a collection of all the elements in the document under IE
You can see the number of elements below the document through Document.all.length.
The index parameter after document.all (index) is a collection method
If index is string then you can get the element with ID or name index under document if there is only one element that returns this element if there is more than one element ID or name is index, return a collection if there is no, return null
This is not good because you are more prone to error when you don't know how many IDs or name is the element of index.
If index is a number, it returns the index element in a 0 based document. The document sequence for the element can be obtained by element.sourceindex
document.getElementById (name) returns only the first element with ID or name name so it is not NULL, it is an element and does not return a collection, which greatly reduces the likelihood of an error occurring
If you need to return an ID or a collection of elements named name, use Document.getelementsbyname (name) to get the
document.getElementById
Returns the first object with the same ID property value as the specified value, and if the ID belongs to a collection, the getElementById method returns the first object in the collection.
document.all
If the ID belongs to a collection, Document.all returns a collection. And only IE is supported.
So under the premise of using IE, document.all (index) to be effective to ensure that index is the only
So if you want your JS script to be used in most of the current browsers, you need to use the [common 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.