美元符号 [英] The $ dollar sign

查看:96
本文介绍了美元符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些简单的东西:

$(selector).append("somestuff");

但是由于我要重用选择器,所以我将其缓存为:

But since I'm going to reuse the selector I cache it with:

var $selector = $(selector);

所以我最终得到:

$selector.append("somestuff");

我的问题是,我应该这样做还是应该这样做:

My question is, should I be doing that, or should I be doing:

var selector = $(selector);
selector.append("somestuff");

尝试任何一个,都可以.哪种方法正确,为什么?因为在$(selector)中已经声明了jquery对象,所以$selector中的$是不必要的吗?

Trying either one, both works. Which method is correct, and why? Is the $ in $selector unnecessary because the jquery object has already been declared in $(selector)?

感谢您的回答.看起来很简单也很清楚.不过,对于是否应在变量中使用$似乎仍存在分歧.每个人都投票赞成答案会很好. :)

Thanks for the answers. It seems very simple and quite clear. Still, there seems to be disagreement over whether or not I should use $ in the variable. It would be nice for everyone to vote up an answer. :)

推荐答案

我已经看过了两种方法.您要做的就是创建一个名为"$ selector"的变量,因此它们在功能上是等效的.有趣的是,它确实使它们很容易被选为jQuery对象.

I've seen it done both ways. All you are doing is creating a variable with the name '$selector', so they are functionally equivalent. The nice thing about it is that it does make them easy to pick out as jQuery objects.

这篇关于美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆