The jquery object and the DOM object are converted to each other

Source: Internet
Author: User

Core tip: The jquery selector gets the jquery object and document.getElementById () in standard JavaScript to get the DOM object is two different object types, typically, such as s (' #id ') Get the jquery object, it cannot use the DOM method in JS. Therefore, if the jquery object is to use the standard Dom method, the object conversion is required.

When you start learning jquery, you may not be able to figure out which jquery objects are and which are DOM objects. As far as DOM objects are not explained, we have too much contact, and the following focuses on jquery and the conversion between the two.

What is a jquery object?

---is the object that is generated after the DOM object is wrapped through jquery. jquery objects are unique to jquery and can be used in the jquery approach.

Like what:

$ ("#test"). html () means: Gets the HTML code within the element with ID test. where HTML () is the method in jquery

This code is equivalent to implementing the code with the DOM:

document.getElementById ("id"). InnerHTML;

Although jquery objects are created after wrapping a DOM object, jquery cannot use any of the DOM objects, nor does the DOM object use the methods in jquery. Disorderly use will be an error. For example: $ ("#test"). InnerHTML, document.getElementById ("id"). html () is wrong.

It is also important to note that the DOM object obtained by the JQuery object and document.getElementById ("id") with #id as the selector is not equivalent. See the conversion between the two below.

Since jquery is a different but also connected, jquery objects and Dom objects can also be converted to each other. Before we convert between the two, we give a contract: if one gets a jquery object, then we add $ to the variable, such as: var $variab = jquery object, and if you get a DOM object, it's the same as usual: var Variab = dom Object , so the agreement is only easy to explain and distinguish, the actual use does not stipulate.

The jquery object turns into a DOM object:

Two conversions convert a jquery object to a DOM object: [index] and. get (index);

(1) JQuery object is a data object, you can get the corresponding Dom object by means of [index].

Such as:

var $v =$ ("#v"); jquery object var v= $v [0]; Dom Object Alert (v.checked)//Detect if this checkbox is selected

 

The DOM object is turned into a jquery object:

For a DOM object, you can just wrap the DOM object with $ () and you'll get a jquery object. $ (DOM object)

Such as:

1 var // DOM Object 2  3 var // jquery Object

After the conversion, you can use the JQuery method arbitrarily.

With the above methods, jquery objects and Dom objects can be converted to each other arbitrarily. It is important to note that DOM objects can use methods in the DOM, and jquery objects are not available in the DOM

 

The jquery object and the DOM object are converted to each other

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.