$(this)和jQuery中的这个有什么区别?

Ami*_*uda 19 jquery this jquery-selectors

jQuery 之间$(this)和之间的区别是什么?this为什么它们有时会给出相同的结果,而其他时候表现不同?

Jus*_*ner 26

$(this)包装thisjQuery功能.

例如,此代码将失败:

$('.someDiv').onClick(function(){
    // this refers to the DOM element so the following line would fail
    this.fadeOut(100);
});
Run Code Online (Sandbox Code Playgroud)

所以我们this用jQuery 包装:

$('.someDiv').onClick(function(){
    // wrap this in jQuery so we can use jQuery fadeOut
    $(this).fadeOut(100);
});
Run Code Online (Sandbox Code Playgroud)


Mat*_*all 10

$(this)this使用jQuery函数装饰任何对象.典型的用例是this引用DOM元素(例如,a <div>).然后,编写$(this)允许您使用所有jQuery API函数<div>.

如果this已经引用了一个jQuery对象 - 通常是一个jQuery装饰的DOM对象 - 那么调用$(this)将没有任何效果,因为它已经被装饰了.