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 = "test.jpg"; here document. getElementById ("img") is the DOM object;
$ ("# Img"). attr ("src", "test.jpg"); and document. getElementById ("img"). src = "test.jpg ";Is equivalent, is correct,However, $ ("# img"). src = "test.jpg"; or document. getElementById ("img"). attr ("src", "test.jpg ");All are errors.
Here is an example: this is what I often write when writing jQuery: this. attr ("src", "test.jpg"); but it is an error. In fact, this is a DOM object, and. 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 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.
2. 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