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

时间:2023-01-22 20:12:38

What's the difference between $(this) and this in jQuery, and why do they sometimes give the same result and other times behave differently?

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

7 个解决方案

#1


26  

$(this) wraps this with the jQuery functionality.

$(this)用jQuery功能包装它。

For example, this code would fail:

例如,此代码将失败:

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

So we wrap this in jQuery:

所以我们将它包装在jQuery中:

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

#2


10  

$(this) decorates whatever object this points to with the jQuery functions. The typical use case is for this to reference a DOM element (say, a <div>). Then, writing $(this) allows you to use all the jQuery API functions on that <div>.

$(this)用jQuery函数装饰这指向的任何对象。典型的用例是为了引用DOM元素(例如,

)。然后,编写$(this)允许您使用该
上的所有jQuery API函数。

If this already refers to a jQuery object - usually a jQuery-decorated DOM object - then calling $(this) will have no effect because it's already decorated.

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

#3


4  

If in your current context if the this is not a jQuery object, you can make it a jQuery element by wrapping it around $(). When your element already is a result of jQuery expression, the this in that case is already a jQuery object. So in that case they both work similarly

如果在当前上下文中,如果这不是jQuery对象,则可以通过将其包装在$()附近来使其成为jQuery元素。当你的元素已经是jQuery表达式的结果时,在这种情况下,这已经是一个jQuery对象。所以在那种情况下,他们的工作方式相似

#4


3  

for you to understand a little bit better, get yourself a debugger of somekind such as google chrome and do this..

为了让你更好地理解,让自己成为像google chrome这样的某种调试者并做到这一点。

$('a').click(function(){
    console.log(this); //DO
    console.log($(this)); //JO
});

this will show you what the difference is :)

这将告诉你有什么区别:)

#5


2  

this is a javascript variable created whenever you are inside a function which is attached to an object. In these cases, this refers to that object.

这是一个javascript变量,只要你在一个连接到一个对象的函数内部就会创建它。在这些情况下,这指的是该对象。

$(this) returns a jQuery object which you can call jQuery functions on, but will apply only to this.

$(this)返回一个jQuery对象,您可以在其上调用jQuery函数,但仅适用于此。

For example, if you set a click handler for all anchors:

例如,如果为所有锚点设置单击处理程序:

$('a').click(function() {
    console.log(this.href) ;
}) ;

then the this, refers to the anchor, which the click event (function) is attached to.

那么这个,指的是附加了click事件(函数)的锚。

#6


1  

$(this) == this ? interesting.

$(this)==这个?有趣。

this must not pass by DOM event.

这不能通过DOM事件传递。

#7


0  

In JavaScript this always refers to the “owner” of the function that is executing. Using $(this) will only wrap the owner so that all the jQuery operations will be extended and available to it.

在JavaScript中,这总是指正在执行的函数的“所有者”。使用$(this)将只包装所有者,以便所有jQuery操作都将被扩展并可用于它。

Consider:

考虑:

$links = $('#content a');

$links.click(function() {
    link  = this;
    $link = $(this); //jQuery wrapped object.

    alert(link.getAttribute('href'));        
    alert($link.attr('href')); //we can use the attr() function from jQuery
});

They usually give the same results since the owner is the same, only that when wrapped by jQuery it can operate with the jQuery functions.

它们通常给出相同的结果,因为所有者是相同的,只有当它被jQuery包装时它才能使用jQuery函数。

#1


26  

$(this) wraps this with the jQuery functionality.

$(this)用jQuery功能包装它。

For example, this code would fail:

例如,此代码将失败:

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

So we wrap this in jQuery:

所以我们将它包装在jQuery中:

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

#2


10  

$(this) decorates whatever object this points to with the jQuery functions. The typical use case is for this to reference a DOM element (say, a <div>). Then, writing $(this) allows you to use all the jQuery API functions on that <div>.

$(this)用jQuery函数装饰这指向的任何对象。典型的用例是为了引用DOM元素(例如,

)。然后,编写$(this)允许您使用该
上的所有jQuery API函数。

If this already refers to a jQuery object - usually a jQuery-decorated DOM object - then calling $(this) will have no effect because it's already decorated.

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

#3


4  

If in your current context if the this is not a jQuery object, you can make it a jQuery element by wrapping it around $(). When your element already is a result of jQuery expression, the this in that case is already a jQuery object. So in that case they both work similarly

如果在当前上下文中,如果这不是jQuery对象,则可以通过将其包装在$()附近来使其成为jQuery元素。当你的元素已经是jQuery表达式的结果时,在这种情况下,这已经是一个jQuery对象。所以在那种情况下,他们的工作方式相似

#4


3  

for you to understand a little bit better, get yourself a debugger of somekind such as google chrome and do this..

为了让你更好地理解,让自己成为像google chrome这样的某种调试者并做到这一点。

$('a').click(function(){
    console.log(this); //DO
    console.log($(this)); //JO
});

this will show you what the difference is :)

这将告诉你有什么区别:)

#5


2  

this is a javascript variable created whenever you are inside a function which is attached to an object. In these cases, this refers to that object.

这是一个javascript变量,只要你在一个连接到一个对象的函数内部就会创建它。在这些情况下,这指的是该对象。

$(this) returns a jQuery object which you can call jQuery functions on, but will apply only to this.

$(this)返回一个jQuery对象,您可以在其上调用jQuery函数,但仅适用于此。

For example, if you set a click handler for all anchors:

例如,如果为所有锚点设置单击处理程序:

$('a').click(function() {
    console.log(this.href) ;
}) ;

then the this, refers to the anchor, which the click event (function) is attached to.

那么这个,指的是附加了click事件(函数)的锚。

#6


1  

$(this) == this ? interesting.

$(this)==这个?有趣。

this must not pass by DOM event.

这不能通过DOM事件传递。

#7


0  

In JavaScript this always refers to the “owner” of the function that is executing. Using $(this) will only wrap the owner so that all the jQuery operations will be extended and available to it.

在JavaScript中,这总是指正在执行的函数的“所有者”。使用$(this)将只包装所有者,以便所有jQuery操作都将被扩展并可用于它。

Consider:

考虑:

$links = $('#content a');

$links.click(function() {
    link  = this;
    $link = $(this); //jQuery wrapped object.

    alert(link.getAttribute('href'));        
    alert($link.attr('href')); //we can use the attr() function from jQuery
});

They usually give the same results since the owner is the same, only that when wrapped by jQuery it can operate with the jQuery functions.

它们通常给出相同的结果,因为所有者是相同的,只有当它被jQuery包装时它才能使用jQuery函数。