What is the difference between these three forms:
this $this $(this)
Explanation: The $(this) selector is used to select current HTML elements. 19.
$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.
In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.
The this is a simple javascript (DOM) object, $(this) will turn the object into a jQuery object. var myHeaderDiv = document. getElementById('header'); $myHeaderDiv = $(myheaderDiv); //just a variable transformed into jQuery object, as with this.
In typical usage you'll usually see them like this (the $this
usage may vary):
this
- Refers to the DOM element in the handler you're currently on, but this may be another object entirely in other situations, but it's always the context.$this
- Usually created by var $this = $(this)
a cached version of the jQuery wrapped version for efficiency (or chain off $(this)
to get the same in many cases).$(this)
- The jQuery wrapped version of the element, so you have access to all its methods (the ones in $.fn
specifically).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