jQuery 中的 $this 与 $(this)

2024-04-04

我看过一些关于SO的讨论$(this) vs $this在 jQuery 中,它们对我来说很有意义。 (看在这里讨论 https://stackoverflow.com/questions/1051782/jquery-this-vs-this举个例子。)

但是下面来自 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.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery 中的 $this 与 $(this) 的相关文章

随机推荐