The conversion between jquery and Dom

Source: Internet
Author: User

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 () isthe 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 object's methods, nor does the DOM object use the methods in jquery. The use of chaos will result in 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
(2) jquery itself provides, through the. Get (Index) method, to get the corresponding DOM object
such as: Var $v =$ ("#v"); jquery Object
var v= $v. Get (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: Var V=document.getelementbyid ("V"); Dom Object
var $v =$ (v); 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.

Here are some other ways to use it:
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 the jquery method can be used.

var test=document.getelementsbytagname (' Li ');
$ (test). CSS (' background ', ' yellow ');

var test=document.getelementsbytagname (' li ') [0];
$ (test). CSS (' background ', ' yellow ');

var test=document.getelementsbytagname (' Li ');
$ (test). EQ (1). css (' background ', ' yellow ');



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

These are DOM objects, and you can use the methods in the DOM, but you can't use jquery any more.

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

The conversion between jquery and 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.