我在 jQuery 中看到过一些关于 $(this)
与 $this
的讨论,它们对我来说很有意义。 (有关示例,请参见 discussion here。)
但是下面的代码片段呢,来自 jQuery 网站插件教程,展示了可链接性是如何工作的?
(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
代表什么?就在我认为我想通了......
最佳答案
$this
只是一个普通变量。 $
字符是变量名称中的有效字符,因此 $this
与任何其他非保留变量名称的作用相同。它在功能上等同于调用变量 JellyBean
。
关于javascript - jQuery 中的 $this 与 $(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7389944/