Using jQuery wrapped methods or properties with $of this

  • 2020-03-30 02:59:22
  • OfStack

If you want to use the attributes or methods of the HTML element itself, you need to use this. If you want to use the jQuery wrapped methods or attributes, you need $(this).
 
$(this)[0] == this; 

The above code is to use this to call the reset method of the form. This method is not supported by jQuery, so there is this.reset(). You can also use $(this)[0].reset().

About when to use both? Here's an example:
 
<a href="http://segmentfault.com/q/1010000000125418" target="_blank" data-id="1010000000125418">jQuery</a> 

 
$('a').click(function(){ 
this.innerHTM==$(this).html()=='jQuery';//All three are the same.
this.getAttribute('href')==this.href==$(this).attr('href')//All three are the same;
this.getAttribute('target')==this.target==$(this).attr('target')//All three are the same;
this.getAttribute('data-id')==$(this).attr('data-id')//It's the same thing;
}); 

Related articles: