Jquery - 何时使用“this”,何时使用“$(this)”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8469635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:34:27  来源:igfitidea点击:

Jquery - When to use "this" and when to use "$(this)"?

jquery

提问by Buttle Butkus

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 }

采纳答案by Joseph Marikle

the thisobject 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对象不会改变。它是函数的所有者。在大多数情况下,它只是一个节点,您可以引用它的所有属性,例如this.className. (把它想象成一个节点或你得到的其他东西document.getElementById)。它只是函数的“所有者”。

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

因此,您只是将this对象传递给 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),只需使用this.

回答by user1040899

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

这是用于 javascript,$(this) 用于 jQuery。您可以将 $(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 :

编辑:对于您的示例, i 只是他所在的递增数字(0 1st 10 11the) $(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 函数。

回答by alex

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

this在 jQuery 中一般指向一个 DOM 元素,例如HTMLSelectElement.

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

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

回答by thescientist

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 引用。

回答by Willem Ellis

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

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

} );

In this example, thisis referring to the .classdiv and would load the contents of file.htmlinto said div when you clicked on it.

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

So click on .class, file.htmlgets loaded into it.

所以点击.classfile.html被加载进去。

回答by Rick Strahl

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

  • 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

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

  • 任何时候你得到一个 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 插件 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 只会返回相同的对象。