For a DOM object, you can simply wrap the DOM object with $ () and you'll get a JQuery object, using [index] and. Get (index) to convert to DOM objects
jquery objects are objects that are produced after wrapping a DOM object through jquery. jquery object is unique to jquery, it can use the method in jquery, but can't 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对象var $v = $(v); //jQuery 对象
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 对象var v = $v[0]; //DOM 对象alert(v.checked); //检测这个checkbox是否被选中
(2) JQuery itself provides, through the. Get (index) method to get the corresponding DOM object
Such as:
var $v = $("#v"); //jQuery 对象var v = $v.get(0); //DOM对象 ( $v.get()[0] 也可以 )alert(v.checked); //检测这个 checkbox 是否被选中
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 JS objects