What is the difference between a DOM object and a jquery object

Source: Internet
Author: User

jquery objects and Dom objects use instructions that need to be referenced by a friend.
1.jQuery Objects and Dom objects
Learning jquery for the first time often distinguishes between jquery objects and Dom objects, so you need to focus on the jquery and Dom objects and the relationships between them.
Dom object, which is the object we get with the traditional method (JavaScript), the jquery object is the object obtained by the jquery class library selector;
The code is as follows:

1 var // DOM Object 2 var // jquery object;


The jquery object is the object that is created after wrapping the DOM object through jquery, which is unique to jquery. If an object is a jquery object, you can use the method in jquery, for example:
1 // gets the HTML code within the element with the ID foo, and the HTML () is a unique method of jquery;

The code above is equivalent to:

1 document.getElementById ("foo"). InnerHTML;

Note: You cannot use any method of a DOM object in a jquery object.
For example $ ("#id"). InnerHTML and $ ("#id"). Checked and the like are all wrong, and can be replaced with a jquery method such as $ ("#id"). HTML () and $ ("#id"). attr ("checked"). Similarly, a DOM object cannot use the jquery method. Learning jquery starts with the right idea, distinguishing between jquery objects and Dom objects, and then learning jquery is a lot easier.
2.jQuery objects and Dom objects are converted to each other
In the 1th above, jquery objects are not the same as DOM objects! For example, the jquery object cannot use the DOM method, the DOM object cannot use the JQuery method, and if I do not encapsulate the method I want, what can I do?
At this point we can convert the Jquer object into a DOM object.
jquery objects converted to DOM objects
jquery provides two ways to convert a jquery object into a DOM object, which is [index] and get (index). Some people might find it strange, how to use subscript, yes, the jquery object is an array object.
The following code shows a way to convert a jquery object into a DOM object and then use the DOM object
The code is as follows:
1 var // jquery Object 2 var // DOM objects can also be written as Var cr= $cr. Get (0); 3 // detects if the checkbox is selected
Dom objects converted into jquery objects
For a DOM object, you simply wrap the DOM object with $ () and you can get a jquery object by $ (DOM object);
The code is as follows:
1 var // DOM Object 2 var // Convert to jquery object
 


finally again, DOM objects can use methods in the DOM, and jquery objects cannot use methods in the DOM, but jquery objects provide a more complete set of tools for manipulating the DOM.

ps:
recommendation: If you get a jquery object, precede the variable with $, which makes it easy to identify which jquery objects are, such as:
var $variable = jquery object;
If you get a DOM object, it is defined as follows:
var variable = Dom Object

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.