jQuery-何时使用"this"以及何时使用"$(this)"? [英] Jquery - When to use "this" and when to use "$(this)"?
问题描述
可能重复:
jQuery $(this)vs this
Possible Duplicate:
jQuery $(this) vs this
"this"和"$(this)"有什么区别?
What is the difference between "this" and "$(this)"?
我怎么知道要使用哪个?
How do I know which one to use?
相关,我认为:
每个都有可选参数. "i"与"this"(或"$(this)")有何不同?
With each, you have the optional parameters. How is the "i" different than "this" (or "$(this)")?
$('img').each(function(i) { ....code }
vs.
$('img').each(function() { ....code }
推荐答案
this
对象不变.它是该函数的所有者.在大多数情况下,它只是一个节点,您可以引用其所有属性,例如this.className
. (将其视为您使用document.getElementById
所获得的节点或其他节点).它只是函数的所有者".
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
对象传递给jQuery的$()
.
Therefore, you are just passing the this
object to jQuery's $()
.
结论:如果要对当前节点使用jQuery函数,请使用$(this)
.但是,如果要访问对象自己的属性(例如.name
,className
,.id
),则只需使用this
.
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"以及何时使用"$(this)"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!