Core tip: The jquery selector gets the jquery object and document.getElementById () in standard JavaScript to get the DOM object is two different object types, typically, such as s (' #id ') Get the jquery object, it cannot use the DOM method in JS. Therefore, if the jquery object is to use the standard Dom method, the object conversion is required.
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 () 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. 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.
jquery objects and Dom object transformations