开发者

Why do people assign $this = $(this) in many jQuery plugins?

I often see this as the first line of a plug-in:

$this = $(this);

Is this just for efficiency, to avoid getting the jQuery object each and every t开发者_如何学JAVAime?


To cache the jQuery object and not have to instantiate it every time they require it.


Just as the above answers says it will cache the object - sort of.

If you call $(this) jQuery will search in the DOM until it finds this-element. If you want to do alot of changes to the element, it will be faster to save the reference to this-element.

$this = $(this);

Now the element is saved as the $this variabel and if you want to do stuff to it again, you just use the variabel.

$this.hide(); //hides the element.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜