What is the difference between document.getelementby ("id") and $ ("#id") _ Basics

Source: Internet
Author: User
The following is a brief introduction:

The first is JS to get the object of the method, more direct, if a page has more than one ID, each time to write so long Document.getelementby ("id")

Copy Code code as follows:

Document.getelementby ("id")


the shorthand method of document.getElementById

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;
}
}


the second is the way to get IDs in jquery.

can refer to this article specifically

Http://www.jb51.net/article/27617.htm

Http://www.jb51.net/article/26702.htm

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.