Home > Article > Web Front-end > Difference between jquery object and dom object
Related recommendations: "jQuery Video Tutorial"
There are a lot of judgment codes in recent work, using jquery and There are many places for dom objects. Let me write them here to strengthen the basic concepts:
1. When judging whether it exists, you must use the dom object, because when jQuery obtains an object, it will be used regardless of whether the object to be obtained exists or not. Return jQuery object
2. Conversion between jquery object and dom object:
The jQuery object is the object (collection object) generated by wrapping the DOM object through jQuery. The jQuery object is unique to jQuery and you can use the methods in jQuery.
Therefore, jQuery objects and DOM objects are different and cannot call methods defined by each other. Therefore, $(‘#test’).innerHTML will report an error, and document.getElementById(‘#test’)[0].html() will also report an error.
Ordinary DOM objects can be wrapped with $() and converted into jQuery objects:
$(document.getElementById('#test')).html();//Normal
jQuery object The jquery object itself is a collection. To be converted into a DOM object, it can be retrieved through the array index:
The first way: $('#test')[0]
The second way: $( '#test').get(0)
Note: eq(0) still returns a jQuery object, and eq(0)[0] is a DOM object.
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of Difference between jquery object and dom object. For more information, please follow other related articles on the PHP Chinese website!