The difference between $ () in jquery and getElementById () in JavaScript

Source: Internet
Author: User

The success of jquery owes much to its powerful selector.

However, I believe that many students who are beginners to jquery will encounter the following problems.

Under JavaScript, we can get page elements based on getElementById (). As follows:

var oDiv1 = document.getElementById ("divID");

It's much easier to use jquery:

var oDiv2 = $ ("#divId");

In general, we would think that these two items return a page element object with an ID of "divID". But in fact, these two things are different:

// return "[Object Htmldivelement]" // returns "[Object Object]"

You can see that ODIV1 is really a page element object, but what about ODIV2?

// effective // does not take effect, proves that ODiv2 does not refer to the page element with id "divID"

Let's look at the browser console to see what they are:

ODiv1 oDiv2< [<div id= "divID";]

You can see that oDiv2 is actually a number of objects!

In fact, thejquery selector returns an array object where the page element object is placed, and getElementById () returns a page element object.

So when we're going to do something like ODiv2, it's going to be like this:

// effective // effective // actually ODIV1 = odiv2[0]

The difference between $ () in jquery and getElementById () in JavaScript

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.