When I first started learning jquery, I may be confused about jquery objects and DOM objects. As for DOM objects, we don't have much to explain, and we have too many contacts. Next we will focus on jquery and the conversion between them.
What is a jquery object?
--- It is the object generated after DOM object is packaged using jquery. Jquery objects are unique to jquery and can be used in jquery.
For example:
$ ("# Test" example .html () means to get the HTML code in the element with the ID of test. Here, HTML () is the method in jquery.
This code is equivalent to implementing code with DOM:
Document. getelementbyid ("ID"). innerhtml;
Although jquery objects are generated after packaging DOM objects, jquery cannot use any methods of DOM objects. Similarly, DOM objects cannot use methods in jquery. If they are used improperly, an error is returned. For example, $ ("# test"). innerhtml and document. getelementbyid ("ID" ).html () are all incorrect.
Note that the DOM object obtained by using the # ID as the selector is the DOM object obtained by the jquery object and document. getelementbyid ("ID"). These two are not equivalent. See the conversion between the two.
Since jquery is different but also related, jquery objects and DOM objects can also be converted to each other. Before the conversion, we first give a convention: If a jquery object is obtained, we add $ before the variable, for example, VAR $ variab = jquery object; if the DOM object is obtained, it is the same as the common convention: var variab = DOM object. This Convention is only convenient for explanation and difference. It is not specified in actual use.
Convert a jquery object to a DOM object:
Two conversion methods are used to convert a jquery object to a DOM object: [Index] And. Get (INDEX );
(1) The jquery object is a data object. You can use the [Index] method to obtain the corresponding DOM object.
For example, VAR $ v = $ ("# V"); // jquery object
VaR v = $ V [0]; // DOM object
Alert (V. Checked) // checks whether the checkbox is selected
(2) jquery provides the. Get (INDEX) method to obtain the corresponding DOM object.
For example, VAR $ v = $ ("# V"); // jquery object
VaR v = $ v. Get (0); // DOM object
Alert (V. Checked) // checks whether the checkbox is selected
Convert a DOM object to a jquery object:
For a DOM object, you only need to wrap the DOM object with $ () to obtain a jquery object. $ (DOM object)
For example, VAR v = Document. getelementbyid ("v"); // DOM object
VaR $ v = $ (V); // jquery object
After conversion, you can use the jquery method at will.
Using the above method, you can convert jquery objects and DOM objects to each other. Note that only DOM objects can use methods in Dom. jquery objects cannot use methods in Dom.