javascript - jQuery 中的 $this 与 $(this)

标签 javascript jquery

我在 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/

相关文章:

javascript - 使用 OAuth 流程进行身份验证后,AdWords 范围 : gapi. client.adwords 丢失。找不到传递给gapi.client.request()的路径变量

javascript - 组中的 D3 路径不显示在 Firefox 中,仅显示在 Chrome 中

javascript - 使用 popup.js 添加一个关闭弹出窗口的按钮

JavaScript split() 方法导致未定义

javascript - while 循环和 "this"对象

javascript - 在 Javascript 中设置延迟

javascript - 是否可以在尚未加载的元素上调用方法?

jquery - 使用 jQuery 更新动态/ajax 表行的替代颜色

javascript - 如何提取href属性中的查询参数?

javascript - 如何在 javaScript 中获取 div 名称?