【发布时间】:2011-06-07 07:03:59
【问题描述】:
我经常把它看作是插件的第一行:
$this = $(this);
这只是为了提高效率,避免每次都获取jQuery对象吗?
【问题讨论】:
标签: javascript jquery jquery-plugins
我经常把它看作是插件的第一行:
$this = $(this);
这只是为了提高效率,避免每次都获取jQuery对象吗?
【问题讨论】:
标签: javascript jquery jquery-plugins
缓存 jQuery 对象,而不必在每次需要时都实例化它。
【讨论】:
正如上面的答案所说,它会缓存对象 - 有点。
如果你调用$(this),jQuery 将在 DOM 中搜索,直到找到 this-element。如果要对元素进行大量更改,保存对this-element 的引用会更快。
$this = $(this);
现在元素被保存为$this 变量,如果你想再次对它做一些事情,你只需使用变量。
$this.hide(); //hides the element.
【讨论】: