jQuery $this vs $(this)在插件开发中

我想知道为什么在这么多的 jquery插件中,$(this)设置为指向$this,这是一个例子,如果我在页面上包含以下两个插件
(function($) {
   jQuery.fn.pluginone = function() {
      return this.each(function() {
         $this = $(this); <--
         alert($this);
      });
   };
})(jQuery)

(function($) {
   jQuery.fn.pluginTwo = function() {
      return this.each(function() {
         $this = $(this); <--
         alert($this);
      });
   };
})(jQuery)

当我在dom上调用两个插件时:

$(document).ready({
   $('.myClass').pluginone();
   $('.myOtherClass').pluginTwo();
});

一个插件将从第二个插件获得$this …而我将$(this)指向一个本地var:

(function($) {
   jQuery.fn.pluginTwo = function() {
      return this.each(function() {
         var trigger = $(this); <--
         alert(trigger);
      });
   };
})(jQuery)

一切都有效,当然应该……

所以我的问题是……我什么时候应该使用$this?

谢谢

解决方法

$这是人们用来在变量中缓存实际jQuery对象$(this)的标准.

你可以把它叫做你想要的,有些人喜欢把它称为$self.

例如

var $self = $(this)

当您查看代码以识别它是一个jQuery对象而不是一个普通的dom对象时,它会有所帮助.

由于您只创建了一个jQuery变量实例,因此性能更好.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...