This is my code:
$myDiv = $('<div>1</div>');
$myDiv.each(function () {
console.log(this.html());
});
It produces an error because this
should be $(this)
. But wait. Isn't $myDiv
a jQuery object in the first place, so that this
must also be a jQuery object. If so, why should I wrap this
inside of $( )
?
A jQuery object is more or less an array of regular DOM elements. each
iterates over these. this
is just a DOM element whereas $(this)
generates a one-element array of DOM elements with access to the jQuery API functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With