The conversion method between jquery object and Dom object _jquery

Source: Internet
Author: User
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 they can use the methods in jquery.
Like what:
$ ("#test"). html () means: Gets the HTML code within the element with the 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 methods of the DOM object, nor can the same DOM object use the method in jquery. Indiscriminate use can be an error. For example: $ ("#test"). InnerHTML, document.getElementById ("id"). The writing of HTML () is wrong.
Another thing to note is that using #id as a selector is not equivalent to getting a DOM object from a jquery object and a document.getElementById ("id"). Please refer to the conversion between the two as described below.
Since jquery is different but also connected, jquery objects and Dom objects can also be converted to each other. Before converting between the two, we give a convention: if one gets a jquery object, then we add $ to the variable, such as: var $variab = jquery object; If you get a DOM object, it's the same as usual: var Variab = dom Object This agreement is only easy to explain and difference, the actual use does not stipulate.

The jquery object is turned into a DOM object:
Two conversions convert a jquery object into a DOM object: [index] and. get (index);
(1) The jquery object is a data object, which can be obtained through the method of [index] to get the corresponding DOM object.
such as: Var $v =$ ("#v"); jquery objects
var v= $v [0]; Dom Object
Alert (v.checked)//Detect if this checkbox is selected
(2) jquery itself provides, through the. Get (Index) method, the corresponding DOM object
such as: Var $v =$ ("#v"); jquery objects
var v= $v. Get (0); Dom Object
Alert (v.checked)//Detect if this checkbox is selected

The DOM object turns into a jquery object:
For already a DOM object, you can get a jquery object by wrapping the DOM object with $ (). $ (DOM object)
such as: Var V=document.getelementbyid ("V"); Dom Object
var $v =$ (v); jquery objects
After the conversion, you can use the JQuery method at will.
The above methods allow you to arbitrarily convert jquery objects and Dom objects to each other. It should be emphasized that DOM objects can use methods in the DOM, and jquery objects are methods that cannot be used in the DOM.

Here are the other related uses:
1. Dom object to jquery object
Ordinary DOM objects can generally be converted to jquery objects by $ ().

such as: $ (document.getElementById ("MSG"))
The jquery object is returned, and you can use the jquery method.

2. jquery Object turn DOM Object
Because the jquery object itself is a collection. So if the jquery object is to be converted to a DOM object, one of these items must be removed, which is generally accessible through the index.
such as: $ ("#msg") [0],$ ("div"). EQ (1) [0],$ ("div"). get () [1],$ ("TD") [5]

These are DOM objects that can use the methods in the DOM, but no longer use the jquery method.

The following are the correct ways to do this:
$ ("#msg"). html ();
$ ("#msg") [0].innerhtml;
$ ("#msg"). EQ (0) [0].innerhtml;
$ ("#msg"). Get (0). InnerHTML;

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.