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.
One, what is a jquery object?
1, is the object that is produced after wrapping the DOM object through jquery. jquery objects are unique to jquery and can be used in the jquery approach.
Like what:
$ ("#test"). HTML ()
This 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.
Like what:
$ ("#test"). InnerHTML, document.getElementById ("id"). html () The notation 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. Please refer to the transfer between the two below
Change.
Since jquery is a different but also connected, jquery objects and Dom objects can also be converted to each other. Before switching between the two we give a contract: if one gets a jquery object, then we are in the variable
Precede with $, such as: var $variab = jquery object; If you get a DOM object, it is the same as the usual: var Variab = dom object; So the convention is just easy to explain and distinguish, actually use
Not specified.
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.
The distinction between jquery objects and Dom objects and the conversion between them