document.getElementById (Get the abbreviated form of ID object) _ Basics

Source: Internet
Author: User
Tags jquery library
For example, use _$ (ID) instead of document.getElementById (ID).
If it's a pure substitution, you can use the following code
Copy Code code as follows:

function $ (ID) {document.getElementById (ID)}

Personal comparison recommends the definition of document.getElementById in prototype:
Copy Code code as follows:

function $ ()
{
var elements = new Array ();
for (var i = 0; i < arguments.length; i++)
{
var element = Arguments[i];
if (typeof element = = ' String ')
element = document.getElementById (element);
if (arguments.length = 1)
return element;
Elements.push (Element);
}
return elements;
}

You can also use the following code to define:
Copy Code code as follows:

function $ (objectId) {
if (document.getElementById && document.getElementById (objectId)) {
return document.getElementById (OBJECTID);//The Consortium DOM
else if (document.all && document.all (objectId)) {
return document.all (OBJECTID);//MSIE 4 DOM
else if (document.layers && Document.layers[objectid]) {
Return document.layers[objectid];//NN 4 DOM. Note:this won ' t find nested layers
} else {
return false;
}
}

This writing can save a lot of characters for JS files, but also can avoid the error of input.
With a little note that $ is a custom value, you can write any non-javascript reserved keyword, such as I_d,myid, and so on, according to your preferences. If your page calls to the jquery library file, you can directly use the $ Instead of document.getElementById.

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.