jquery objects are objects that are produced after wrapping a DOM object through jquery. jquery objects are unique to jquery and can be used in jquery, but cannot use the DOM method, for example: $ ("#img"). attr ("src", "test.jpg"); The $ ("#img") here is the JQuery object.
Dom objects are some of the inherent object operations of JavaScript. DOM objects can use JavaScript's inherent methods, but 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 all wrong.
Another example is this, which is often written when writing jQuery: this.attr ("src", "test.jpg"), but it is a DOM object, and. attr ("src", "test.jpg") is the jquery method , so something went wrong. To solve this problem, convert the DOM object to a JQuery object, such as $ (this). attr ("src", "test.jpg");
1. DOM object into JQuery object
For a DOM object, you just need to wrap the DOM object with $ () and you can get a JQuery object, $ (DOM object) Note: VAR is a variable defined
such as: var v = document.getElementById ("V"); Dom Object
var $v = $ (v); JQuery Object
After conversion, you can use JQuery in any way.
2. JQuery objects into DOM objects
Two ways to convert a JQuery object into a DOM object: [index] and. get (index);
(1) JQuery object is a data object, you can get the corresponding DOM object by means of [index].
such as: var $v = $ ("#v"); JQuery Object
var v = $v [0]; DOM Object
alert (v.checked); Detects if the checkbox is selected
(2) JQuery itself provides, through the. Get (index) method to get the corresponding DOM object
such as: var $v = $ ("#v"); JQuery Object
var v = $v. Get (0); Dom Object ($v. get () [0] can also)
alert (v.checked); Detects if the checkbox is selected
With these methods, jquery objects and Dom objects can be converted arbitrarily to each other, and it is emphasized that DOM objects can use methods in the DOM, and jquery objects are not available in the DOM.
JQuery objects and JavaScript objects