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 objects and Dom objects and the relationships between them.
Dom object, that is, the object that we get from the traditional method (JavaScript), the jquery object is the object that is obtained by using the jquery class library selector;
Copy Code code as follows:
var domobj = document.getElementById ("id"); Dom Object
var $obj = $ ("#id"); jquery object;
The jquery object is the object that is generated by wrapping the DOM object through jquery, which is unique to jquery. If an object is a jquery object, then you can use the method in jquery, for example:
$ ("#foo"). html (); Gets the HTML code within the element with the ID foo, HTML () is a jquery-specific method;
The code above is equivalent to:
document.getElementById ("foo"). InnerHTML;
Note: You cannot use any of the methods of a DOM object in a jquery object.
For example $ ("#id"). innerHTML and $ ("#id"). Checked and the like are wrong, you can use the $ ("#id"). HTML () and $ ("#id"). attr ("checked"), such as the jquery method to replace. Similarly, a DOM object cannot use the jquery method. Learning jquery should start with the right idea to distinguish between the jquery object and the DOM object, and then it will be much easier to learn jquery.
convert 2.jQuery objects to and from DOM objects
In the 1th above, the jquery object and the DOM object are different! For example, the jquery object cannot use the DOM method, the DOM object cannot use the jquery method, then if my jquery does not encapsulate the method which I want, what can I do?
Then we can convert the Jquer object into a DOM object
jquery object converted to DOM object
jquery provides two ways to convert a jquery object into a DOM object, that is, [index] and get (index). Some people may find it strange to use subscript, yes, the jquery object is an array object.
The following code will show you how to convert a jquery object to a DOM object, and then use the DOM object
Copy Code code as follows:
var $cr =$ ("#cr"); jquery objects
var cr = $CR [0]; Dom objects can also be written as Var cr= $cr. Get (0);
alert (cr.checked); Check to see if this checkbox is checked
Dom object converted to jquery object
For a DOM object, you can get a jquery object by wrapping the DOM object with $ (), which means $ (DOM object);
Copy Code code as follows:
var Cr=document.getelementbyid ("Cr"); Dom Object
var $CR = $ (CR); Convert to jquery Object
The methods in jquery can be used arbitrarily after conversion.
The above methods allow you to arbitrarily convert jquery objects and Dom objects to each other.
Finally, again, DOM objects can use methods in the DOM, and jquery objects cannot use methods in the DOM, but jquery objects provide a better set of tools for manipulating the DOM, and DOM operations on jquery are explained in more detail later in this article.
PS: The most commonly used jquery objects are created by the $ () function, and the $ () function is the manufacturing facility of a jquery object.
Recommendation: If you get an object that is a jquery object, precede the variable with $, which makes it easy to identify which jquery objects are, such as:
var $variable = jquery object;
If you get a DOM object, it is defined as follows:
var variable = dom Object