jquery objects and Dom objects use instructions that need to be referenced by a friend.
1.jQuery Objects and Dom objects
Learning jquery for the first time often distinguishes between jquery objects and Dom objects, so you need to focus on the jquery and Dom objects and the relationships between them.
Dom object, which is the object we get with the traditional method (JavaScript), the jquery object is the object obtained by the jquery class library selector;
Copy the code code as follows:
var domobj = document.getElementById ("id"); Dom Object
var $obj = $ ("#id"); jquery object;
The jquery object is the object that is created after wrapping the DOM object through jquery, which is unique to jquery. If an object is a jquery object, you can use the method in jquery, for example:
$ ("#foo"). html (); Gets the HTML code within the element with the ID foo, and the HTML () is a unique method of jquery;
The code above is equivalent to:
document.getElementById ("foo"). InnerHTML;
Note: You cannot use any method of a DOM object in a jquery object.
For example $ ("#id"). InnerHTML and $ ("#id"). Checked and the like are all wrong, and can be replaced with a jquery method such as $ ("#id"). HTML () and $ ("#id"). attr ("checked"). Similarly, a DOM object cannot use the jquery method. Learning jquery starts with the right idea, distinguishing between jquery objects and Dom objects, and then learning jquery is a lot easier.
2.jQuery objects and Dom objects are converted to each other
In the 1th above, jquery objects are not the same as DOM objects! For example, the jquery object cannot use the DOM method, the DOM object cannot use the JQuery method, and if I do not encapsulate the method I want, what can I do?
At this point we can convert the Jquer object to a DOM object
jquery objects converted to DOM objects
jquery provides two ways to convert a jquery object into a DOM object, which is [index] and get (index). Some people might find it strange, how to use subscript, yes, the jquery object is an array object.
The following code shows a way to convert a jquery object into a DOM object and then use the DOM object
Copy the code code as follows:
var $cr =$ ("#cr"); jquery Object
var cr = $CR [0]; Dom objects can also be written as Var cr= $cr. Get (0);
alert (cr.checked); Detects if the checkbox is selected
Dom objects converted into jquery objects
For a DOM object, you simply wrap the DOM object with $ () and you can get a jquery object by $ (DOM object);
Copy the code code as follows:
var Cr=document.getelementbyid ("Cr"); Dom Object
var $CR = $ (CR); Convert to jquery Object
The method of jquery can be used arbitrarily after the conversion.
With the above methods, jquery objects and Dom objects can be converted to each other arbitrarily.
Finally, again, the DOM object can use the methods in the DOM, the jquery object cannot use the methods in the DOM, but the jquery object provides a more complete set of tools for manipulating the DOM, and the DOM operations on jquery are explained in detail later in the article.
PS: The usual jquery objects are created by the $ () function, and the $ () function is a manufacturing facility for jquery objects.
Recommendation: If you get a jquery object, precede the variable with $, which makes it easy to identify what jquery objects are, such as:
var $variable = jquery object;
If you get a DOM object, it is defined as follows:
var variable = dom Object
What is the difference between a DOM object and a jquery object