The jquery object is the object that is generated after the DOM object is wrapped through jquery. jquery objects are unique to jquery, which can use the methods in jquery, but cannot use DOM methods, such as: $ ("#img"). attr ("src", "test.jpg"); The $ ("#img") here is the JQuery object.
Dom objects are some of the object operations inherent to JavaScript. DOM objects can use the intrinsic methods of JavaScript, but they cannot use the methods in JQuery. For example: document.getElementById ("img"). src = "test.jpg"; here the document.getElementById ("img") is the DOM object.
$ ("#img"). attr ("src", "test.jpg"); and document.getElementById ("img"). src = "test.jpg"; is equivalent, is correct, but $ ("#img"). src = "test.jpg" or document.getElementById ("img"). attr ("src", "test.jpg"); are wrong.
Another example is this, which is often written in jquery: This.attr ("src", "test.jpg"); but it's an error, actually this is a DOM object, and. attr ("src", "test.jpg") is a jquery method , so it went wrong. To solve this problem, convert the DOM object into a JQuery object, such as $ (this). attr ("src", "test.jpg");
1. DOM objects Turn into JQuery objects
For a DOM object, you can get a JQuery object by wrapping the DOM object with $ (), $ (DOM object) Note: VAR is defined as a variable
Such as:
var v = document.getElementById ("V"); Dom Object
var $v = $ (v);//jquery Object
After conversion, you can use the JQuery method as you like.
2. JQuery objects Turn into DOM objects
Two conversions speak of a JQuery object converted to a DOM object: [index] and. get (index);
(1) The JQuery object is a data object, which can be obtained through the method of [index] to get the corresponding DOM object.
Such as:
var $v = $ ("#v"); JQuery Object
var v = $v [0];//dom object
alert (v.checked);//Detect if this checkbox is selected
(2) JQuery itself provides the corresponding DOM object through the. Get (Index) method
Such as:
var $v = $ ("#v"); JQuery Object
var v = $v. Get (0);//dom object ($v. got () [0] can also)
alert (v.checked);//Detect if the checkbox is selected
By using these methods, you can arbitrarily transform the jquery objects and Dom objects, and it is necessary to emphasize that DOM objects can use methods in the DOM, and JQuery objects are methods that cannot be used in the DOM.