What is a jQuery object?
--- is the object generated by wrapping the DOM object through jQuery. jQuery objects are unique to jQuery and can use methods in jQuery.
Like what:
$("#test").html() means to get the html code inside the element with the ID test as test. html() is the method in jQuery
This code is equivalent to implementing code in the DOM:
document.getElementById("id").innerHTML;
Although jQuery objects are generated by wrapping DOM objects, jQuery cannot use any methods of DOM objects, nor can DOM objects use methods in jQuery. Random use will result in an error. For example: $("#test").innerHTML, document.getElementById("id").html() are all wrong.
Another thing to note is that using #id as a selector is a jQuery object and a DOM object obtained by document.getElementById("id"), which are not equivalent. See the following for the conversion between the two.
Since jQuery is different but also related, jQuery objects and DOM objects can also be converted to each other. Before converting the two, we first give a convention: if one is a jQuery object, then we add $ in front of the variable, such as: var $variab = jQuery object; If you get a DOM object, it's the same as normal: var variab = DOM object; Such an agreement is only for the convenience of explanation and distinction, and is not stipulated in actual use.
jQuery object to DOM object:
There are two conversion methods to convert a jQuery object into a DOM object: [index] and .get(index);
(1) The jQuery object is a data object, which can be obtained by using the [index] method.
For example: var $v =$("#v"); jQuery object
var v=$v[0]; DOM object
alert(v.checked) // Detects if this checkbox is checked
(2) jQuery itself provides the corresponding DOM object through the .get(index) method
For example: var $v=$("#v"); jQuery object
var v=$v.get(0); DOM object
alert(v.checked) // Detects if this checkbox is checked
DOM object to jQuery object:
For DOM objects, you only need to wrap the DOM object with $() to get a jQuery object. $(DOM object)
For example: var v=document.getElementById("v"); DOM object
var $v=$(v); jQuery object
After the conversion, you can use jQuery's methods as you like.
By using the above methods, you can arbitrarily convert jQuery objects and DOM objects to each other. It should be emphasized that DOM objects can only use methods in the DOM, and jQuery objects cannot use methods in the DOM. |