Introduction to methods for converting jquery objects and Dom objects to each other _jquery

Source: Internet
Author: User

Before discussing the mutual exchange of jquery objects and Dom objects, agree on the style of defining variables. If you get an object that is a jquery object, add $ before the variable, for example:

Copy Code code as follows:

var $variable = jquery object;

If you get a DOM object, it is defined as follows:
Copy Code code as follows:

var variable = dom object;

1.jQuery objects into DOM objects

jquery objects cannot use methods in the DOM, but if you are unfamiliar with the methods provided by the JQuery object, or if jquery does not encapsulate the methods you want, and you have to practice DOM objects, there are two ways to do this. jquery provides two ways to convert a jquery object into a DOM object, that is, [index] and get (index).

(1) The JQuery object is an array object, and the corresponding DOM object can be obtained by the method [index].

The jquery code is as follows:

Copy Code code as follows:

var $CR = $ ("#cr"); jquery objects
var cr = $CR [0]//dom Object
Alert (cr.checked)//Detect if the checkbox is selected

(2) Another method is provided by jquery itself, and the corresponding DOM object is obtained through the Get (index) method.

The jquery code is as follows:

Copy Code code as follows:

[Js]var $CR = $ ("#cr");
var cr = $cr. Get (0);
Alert (cr.checked)

2.DOM object converted to jquery object

For a DOM object, it takes $ () to wrap the DOM object in order to get a jquery object, in the form of $ (DOM object).

The jquery code is as follows:

Copy Code code as follows:

var cr = document.getElementById ("Cr"); Dom Object
var $CR = $ (CR);

After conversion, you can use the methods in jquery in any way you want.

The above methods allow you to arbitrarily convert jquery objects and Dom objects to each other.

Finally, it is emphasized that DOM objects can use methods in the DOM, and jquery objects cannot use methods in the DOM, but the jquery object provides a more sophisticated tool for manipulating DOM

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.