Jquery - 何时使用“this”以及何时使用“$(this)”? [重复]

时间:2021-08-26 22:36:15

Possible Duplicate:
jQuery $(this) vs this

可能重复:jQuery $(this)vs this

What is the difference between "this" and "$(this)"?

“this”和“$(this)”有什么区别?

How do I know which one to use?

我怎么知道使用哪一个?

Related, I think:

相关,我认为:

With each, you have the optional parameters. How is the "i" different than "this" (or "$(this)")?

每个都有可选参数。 “i”与“this”(或“$(this)”)有什么不同?

$('img').each(function(i) { ....code }

vs.

$('img').each(function() { ....code }

6 个解决方案

#1


12  

the this object doesn't change. It is the owner of the function. It is, in most cases like this, simply a node and you can reference all of its properties like this.className. (think of it as you would a node or whatnot that you get with document.getElementById). It is just the "owner" of the function.

这个对象不会改变。它是该功能的所有者。在大多数情况下,它只是一个节点,您可以引用它的所有属性,如this.className。 (把它想象成一个节点或者你用document.getElementById得到的东西)。它只是该功能的“拥有者”。

Therefore, you are just passing the this object to jQuery's $().

因此,您只是将此对象传递给jQuery的$()。

Conclusion: If you want to use jQuery functions for the current node, use $(this). But if you want to access the objects own properties (e.g. .name, className, .id), use simply this.

结论:如果要对当前节点使用jQuery函数,请使用$(this)。但是如果要访问对象自己的属性(例如.name,className,.id),请使用它。

#2


5  

this is for javascript, $(this) for jQuery. you can use $(this) for every functions of jQuery, not the case for this.

这是针对javascript的javascript,$(this)。你可以使用$(this)来表示jQuery的每个函数,而不是这个。

Edit: For your example the i is just the incremented number that he is on (0 the 1st 10 the 11the) the $(this) is the element img precisely you can do either :

编辑:对于你的例子,我只是他所加的数字增加的数字(0,第1 10和11),$(this)是元素img,你可以做到:

$(this).on('click', function() { console.log(123); });
or
$('img').eq(i).on('click', function() { console.log(123); });

Edit2: Here is a usage of this:

Edit2:这是一个用法:

var sorter = {
    sort: function() {
        console.log('sorting');
    },
    requestSorting: function() {
        this.sort();
    }
}
sorter.requestSorting.bind(sorter);

In this example it's exactly used like the $this in PHP class. That's why I said it's more for pure javascript functions.

在这个例子中,它与PHP类中的$ this完全一样。这就是为什么我说纯粹的javascript函数更多的原因。

#3


2  

this in jQuery generally points to a DOM element, such as HTMLSelectElement.

这在jQuery中通常指向DOM元素,例如HTMLSelectElement。

Rewrapping it with the jQuery function allows you to call jQuery methods on it.

使用jQuery函数重新打包它允许您在其上调用jQuery方法。

#4


1  

Putting this inside $() turns this into a jquery object, with the ability to have all the typical jQuery methods called on it. this by itself it just a normal javascript reference to a given object/element.

把它放在$()里面会把它变成一个jquery对象,能够在它上面调用所有典型的jQuery方法。这本身就只是对给定对象/元素的正常javascript引用。

#5


0  

$( ".class" ).click( function () {

  $( this ).load( "/path/to/file.html" );

} );

In this example, this is referring to the .class div and would load the contents of file.html into said div when you clicked on it.

在这个例子中,这是指.class div,并且当你点击它时会将file.html的内容加载到所说的div中。

So click on .class, file.html gets loaded into it.

所以点击.class,将file.html加载到其中。

#6


0  

Whether you need $(this) or not depends on the context you're working in.

您是否需要$(this)取决于您正在使用的上下文。

Need it:

  • When handling jQuery Events this is passed as the DOM element that triggered the event $(this) turns DOM element into a jQuery object

    处理jQuery Events时,它会作为触发事件的DOM元素传递$(this)将DOM元素转换为jQuery对象

  • Anytime you get a DOM element Other instances where you have a DOM element (or an object) and want to turn it into a jQuery object

    任何时候你得到一个DOM元素你有一个DOM元素(或一个对象)并希望将其转换为jQuery对象的其他实例

Don't need it:

不需要它:

  • Inside of a jQuery Plug-in jQuery plug-in (jquery.fn) function this is a jQuery object already so no need to apply $(this) in here.
  • 在jQuery Plug-in jQuery插件(jquery.fn)函数内部,这是一个jQuery对象,所以不需要在这里应用$(this)。

No harm done in $(this) even if this is a jQuery object because jQuery will just return the same object.

即使这是一个jQuery对象也没有在$(this)中造成伤害,因为jQuery只会返回相同的对象。

#1


12  

the this object doesn't change. It is the owner of the function. It is, in most cases like this, simply a node and you can reference all of its properties like this.className. (think of it as you would a node or whatnot that you get with document.getElementById). It is just the "owner" of the function.

这个对象不会改变。它是该功能的所有者。在大多数情况下,它只是一个节点,您可以引用它的所有属性,如this.className。 (把它想象成一个节点或者你用document.getElementById得到的东西)。它只是该功能的“拥有者”。

Therefore, you are just passing the this object to jQuery's $().

因此,您只是将此对象传递给jQuery的$()。

Conclusion: If you want to use jQuery functions for the current node, use $(this). But if you want to access the objects own properties (e.g. .name, className, .id), use simply this.

结论:如果要对当前节点使用jQuery函数,请使用$(this)。但是如果要访问对象自己的属性(例如.name,className,.id),请使用它。

#2


5  

this is for javascript, $(this) for jQuery. you can use $(this) for every functions of jQuery, not the case for this.

这是针对javascript的javascript,$(this)。你可以使用$(this)来表示jQuery的每个函数,而不是这个。

Edit: For your example the i is just the incremented number that he is on (0 the 1st 10 the 11the) the $(this) is the element img precisely you can do either :

编辑:对于你的例子,我只是他所加的数字增加的数字(0,第1 10和11),$(this)是元素img,你可以做到:

$(this).on('click', function() { console.log(123); });
or
$('img').eq(i).on('click', function() { console.log(123); });

Edit2: Here is a usage of this:

Edit2:这是一个用法:

var sorter = {
    sort: function() {
        console.log('sorting');
    },
    requestSorting: function() {
        this.sort();
    }
}
sorter.requestSorting.bind(sorter);

In this example it's exactly used like the $this in PHP class. That's why I said it's more for pure javascript functions.

在这个例子中,它与PHP类中的$ this完全一样。这就是为什么我说纯粹的javascript函数更多的原因。

#3


2  

this in jQuery generally points to a DOM element, such as HTMLSelectElement.

这在jQuery中通常指向DOM元素,例如HTMLSelectElement。

Rewrapping it with the jQuery function allows you to call jQuery methods on it.

使用jQuery函数重新打包它允许您在其上调用jQuery方法。

#4


1  

Putting this inside $() turns this into a jquery object, with the ability to have all the typical jQuery methods called on it. this by itself it just a normal javascript reference to a given object/element.

把它放在$()里面会把它变成一个jquery对象,能够在它上面调用所有典型的jQuery方法。这本身就只是对给定对象/元素的正常javascript引用。

#5


0  

$( ".class" ).click( function () {

  $( this ).load( "/path/to/file.html" );

} );

In this example, this is referring to the .class div and would load the contents of file.html into said div when you clicked on it.

在这个例子中,这是指.class div,并且当你点击它时会将file.html的内容加载到所说的div中。

So click on .class, file.html gets loaded into it.

所以点击.class,将file.html加载到其中。

#6


0  

Whether you need $(this) or not depends on the context you're working in.

您是否需要$(this)取决于您正在使用的上下文。

Need it:

  • When handling jQuery Events this is passed as the DOM element that triggered the event $(this) turns DOM element into a jQuery object

    处理jQuery Events时,它会作为触发事件的DOM元素传递$(this)将DOM元素转换为jQuery对象

  • Anytime you get a DOM element Other instances where you have a DOM element (or an object) and want to turn it into a jQuery object

    任何时候你得到一个DOM元素你有一个DOM元素(或一个对象)并希望将其转换为jQuery对象的其他实例

Don't need it:

不需要它:

  • Inside of a jQuery Plug-in jQuery plug-in (jquery.fn) function this is a jQuery object already so no need to apply $(this) in here.
  • 在jQuery Plug-in jQuery插件(jquery.fn)函数内部,这是一个jQuery对象,所以不需要在这里应用$(this)。

No harm done in $(this) even if this is a jQuery object because jQuery will just return the same object.

即使这是一个jQuery对象也没有在$(this)中造成伤害,因为jQuery只会返回相同的对象。