在jQuery中$ this vs $(this) [英] $this vs $(this) in jQuery
问题描述
我在jQuery中看到了关于 $(this)
vs $ this
的SO的讨论,他们对我有意义。 (有关示例,请参见此处讨论。)
I've seen some discussions on SO regarding $(this)
vs $this
in jQuery, and they make sense to me. (See discussion here for an example.)
但是下面的代码片段,来自jQuery网站插件教程,展示了可链接性如何工作?
But what about the snippet below, from the jQuery website plugin tutorial showing how chainability works?
(function ($) {
$.fn.lockDimensions = function (type) {
return this.each(function () {
var $this = $(this);
if (!type || type == 'width') {
$this.width($this.width());
}
if (!type || type == 'height') {
$this.height($this.height());
}
});
};
})(jQuery);
$ this
代表什么?就在我认为我发现它的时候......
What does $this
represent above? Just when I think I have it figured out ...
推荐答案
$ this
只是一个普通的变量。 $
字符是变量名中的有效字符,因此 $ this
的行为与任何其他非保留变量相同名称。它在功能上与调用变量 JellyBean
完全相同。
$this
is just an ordinary variable. The $
character is a valid character in variable names, so $this
acts the same as any other non-reserved variable name. It's functionally identical to calling a variable JellyBean
.
这篇关于在jQuery中$ this vs $(this)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!