Distinguishing between jquery objects and Dom objects

Source: Internet
Author: User

Before the discussion, make a pact to define the style of the variable.

If the object being obtained is a jquery object, add $ before the variable, for example:

var $variable = jquery object;

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

var variable = dom object;

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

1, jquery object is an array object, you can get the corresponding Dom object by means of [index].

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

2. Get the corresponding Dom object by the Get (index) method.

var $CR = $ ("#cr"), var cr = $cr. Get (0); alert (cr.checked)  ;

For a DOM object, you simply wrap the DOM object with $ (), and you can get a jquery object in the form of $ (DOM object).

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

In this summary note:

The Get method in the jquery method is actually getting the DOM element ($ (this). Get (0) with $ (this) [0])

In the jquery method, the EQ, first, last, and so on are all returned jquery objects.

Dom objects can use methods in the DOM, and jquery objects cannot use the methods in the DOM. The jquery object provides a more complete set of tools for manipulating the DOM.

Distinguishing between jquery objects and Dom objects

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.