Difference between $(this) and this in jquery

2019-01-07 15:23发布

What is the fundamental difference between using $(this) vs this

$('.viewComments').click(function(ev){
    //returns the desired value
    alert(this.getAttribute('id'));

    //Gives an error sayin function is not defined 
    alert($(this).getAttribute('id'));

    //returns the desired value
    alert($(this).attr('id'));
});

What I thought was "$(this)" will contain all functions that "this" has and more..But that doesn't seem to be the case.

So what exactly is $(this)? and

Hw do I know what functions are available when I'm using it? (I know I can get them through firebug. but I would like to know if there any some other way- some doc may be)

标签: jquery this
7条回答
祖国的老花朵
2楼-- · 2019-01-07 16:22

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

查看更多
登录 后发表回答