jQuery 或 JavaScript 中的“$”符号是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8667736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the "$" sign mean in jQuery or JavaScript?
提问by GeekMasher
Possible Duplicate:
What is the meaning of “$” sign in JavaScript
可能的重复:
JavaScript 中的“$”符号是什么意思
Now this must seem to be an easy and stupid question to ask but I must know why we use the dollar ($
) symbol in jQuery and JavaScript.
I always put a dollar in my scripts but I actuary don't know why.
现在这似乎是一个简单而愚蠢的问题,但我必须知道为什么我们$
在 jQuery 和 JavaScript 中使用美元 ( ) 符号。我总是在脚本中投入一美元,但精算师不知道为什么。
For an example:
例如:
$('#Text').click(function () {
$('#Text').css('color', 'red')
});
This just changes the text colour when you click it, but it demonstrates my point.
这只是在您单击时更改文本颜色,但它证明了我的观点。
采纳答案by Quentin
In JavaScript it has no special significance (no more than a
or Q
anyway). It is just an uninformative variable name.
在 JavaScript 中,它没有特别的意义(仅此而已a
或Q
无论如何)。它只是一个无信息的变量名称。
In jQuery the variable is assigned a copy of the jQuery
function. This function is heavily overloaded and means half a dozen different things depending on what arguments it is passed. In this particular example you are passing it a string that contains a selector, so the function means "Create a jQuery object containing the element with the id Text".
在 jQuery 中,变量被分配了一个jQuery
函数的副本。这个函数重载了,根据它传递的参数有六种不同的含义。在这个特定的例子中,你向它传递一个包含选择器的字符串,所以该函数的意思是“创建一个包含具有 id文本元素的 jQuery 对象”。
回答by dflemstr
The $
is just a function. It is actually an alias for the function called jQuery
, so your code can be written like this with the exact same results:
这$
只是一个函数。它实际上是被调用的函数的别名jQuery
,因此您的代码可以像这样编写,结果完全相同:
jQuery('#Text').click(function () {
jQuery('#Text').css('color', 'red');
});
回答by Alberto Solano
In jQuery, the $ sign is just an alias to jQuery()
, then an alias to a function.
在 jQuery 中,$ 符号只是 的别名jQuery()
,然后是函数的别名。
Thispage reports:
这页的报告:
Basic syntax is: $(selector).action()
- A dollar sign to define jQuery
- A (selector) to "query (or find)" HTML elements
- A jQuery action() to be performed on the element(s)
基本语法是:$(selector).action()
- 定义 jQuery 的美元符号
- 用于“查询(或查找)”HTML 元素的(选择器)
- 要在元素上执行的 jQuery action()
回答by Anuj Balan
The jQuery syntax is tailor made for selecting HTML elements and perform some action on the element(s).
jQuery 语法是为选择 HTML 元素和对元素执行某些操作而定制的。
Basic syntax is: $(selector).action()
基本语法是:$(selector).action()
A dollar sign to define jQuery A (selector) to "query (or find)" HTML elements A jQuery action() to be performed on the element(s)
用于定义 jQuery A(选择器)以“查询(或查找)”HTML 元素的美元符号要在元素上执行的 jQuery action()
回答by ExtremeCoder
The $
symbol simply invokes the jQuery library's selector functionality. So $("#Text")
returns the jQuery object for the Text
div
which can then be modified.
该$
符号只是调用 jQuery 库的选择器功能。因此$("#Text")
返回Text
div
可以修改的 jQuery 对象。
回答by Matmarbon
Additional to the jQuery thing treated in the other answers there is another meaning in JavaScript - as prefix for the RegExp properties representing matches, for example:
除了在其他答案中处理的 jQuery 事物之外,JavaScript 中还有另一个含义 - 作为表示匹配项的 RegExp 属性的前缀,例如:
"test".match( /t(e)st/ );
alert( RegExp. );
will alert "e"
会提醒“e”
But also here it's not "magic" but simply part of the properties name
但这里也不是“魔法”,而只是属性名称的一部分