For a DOM object, you only need to wrap the DOM object with $ () to obtain a jQuery object, using [index] and. get (index) can be converted into DOM objects. jQuery objects are generated after DOM objects are packaged by jQuery. JQuery objects are unique to jQuery. They can use methods in jQuery, but do not use DOM methods. For example: $ ("# img "). attr ("src", "test.jpg"); $ ("# img") Here is the jQuery object.
DOM objects are some object operations inherent in Javascript. DOM objects can use Javascript's inherent methods, but they cannot use the methods in jQuery. For example, document. getElementById ("img"). src = sharetest.jpg "; here document. getElementById (" img ") is the DOM object.
$ ("# Img "). attr ("src", "test.jpg"); and document. getElementById ("img "). src = "test.jpg"; is equivalent and correct, but $ ("# img "). src = "test.jpg"; or document. getElementById ("img "). attr ("src", "test.jpg"); all errors.
Another example is this, which is often written like this when writing jQuery. attr ("src", "test.jpg"); but an error occurs. In fact, this is a DOM object. attr ("src", "test.jpg") is a jQuery method, so an error occurs. To solve this problem, we need to convert the DOM object to a jQuery object, such as $ (this). attr ("src", "test.jpg ");
1. convert a DOM object to a jQuery object
For a DOM object, you only need to wrap the DOM object with $ () to get a jQuery object. $ (DOM object) Note: var is the variable defined.
For example:
Var v = document. getElementById ("v"); // DOM object var $ v =$ (v); // jQuery object
After conversion, you can use the jQuery method at will.
2. convert a jQuery object to a DOM object
Two conversion methods: converting 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 itself provides a DOM object that can be obtained through the. get (index) method.
For example:
Var $ v = $ ("# v"); // jQuery object var v = $ v. get (0); // DOM object ($ v. get () [0] can also) alert (v. checked); // checks whether the checkbox is selected.
Through the above method, jQuery objects and DOM objects can be converted to each other. It must be emphasized that DOM objects can use methods in the DOM. jQuery objects cannot use methods in the DOM.