Difference between $(this) and this in jquery

微笑、不失礼 提交于 2019-11-27 03:47:10

this is the DOM object, whereas $(this) is the jQuery wrapper around same.

When using this, you can call DOM methods on it, but not jQuery methods. When using $(this), you can call jQuery methods on it, but not DOM methods.

$(this) - represent current DOM element on which event this function is called

The this keyword - In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of.

each

In jQuery, this refers to the DOM object, and $(this) refers to the same object but with jQuery methods added

you can't call this.each() because each is not a DOM method, its a jquery method

you can call $(this).each() because $(this) returns a jquery object

Here are two articles that you may find helpful:

What is this? by Mike Alsup

jQuery's this: demystified by Remy Sharp

$(this) is the current object that was selected using a jQuery selector or event attached to the object.

so if you have $('#myelement').click(..... then $(this) referes to the element that was clicked on so that $(this).hide() hides that element.

in jQuery the $() notation is a shorthand for the jQuery selector, so if you say $(this) you are asking jQuery to re-select your object. Then you have the usual jQuery functions available. "this" is the object selected by the outer jQuery call.

$(this) is a jQuery object and you can use the power and beauty of jQuery, but with 'this' keyword, one need to use native JavaScript.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!