An analysis of the differences between Jquery objects and Dom objects

  • 2020-03-30 04:21:03
  • OfStack

Prior to the discussion, agree on the style of defining variables.

If the object you get is a jQuery object, put a $before the variable, for example:


var $variable = jQuery object ;

If the DOM object is obtained, the definition is as follows:


var variable = DOM Object;

 
JQuery objects cannot use methods in the DOM, but if you are not familiar with the methods provided by jQuery objects, or jQuery does not encapsulate the methods you want, you have to use DOM objects in the following two ways. JQuery provides two methods for converting a jQuery object into a DOm object: [index] and get (index).
 
1. JQuery object is an array object, and the corresponding DOM object can be obtained by the method of [index].


var $cr = $("#cr"); //JQuery object
var cr = $cr[0] //DOM object
alert(cr.checked) //Check that the checkbox is checked for

2. Get (index) method to get the corresponding DOM object.


var $cr = $("#cr");
var cr = $cr.get(0);
alert(cr.checked);

For a DOM object, just wrap the DOM object with $() to get a jQuery object.


var cr = document.getElementByID("cr"); //DOM object
var $cr = $(cr);
 

Conclusion:
 
1. The get method in jQuery method is actually to get Dom elements ($(this). Get (0) and $(this)[0]).
 
Second, eq, first, last and other methods in jQuery methods are returned jQuery objects
 
DOM objects can only use methods in the DOM. JQuery objects cannot use methods in the DOM. JQuery objects provide a more sophisticated set of tools for manipulating the DOM.


Related articles: