在jQuery或JavaScript中,“$”符号意味着什么?(复制)

时间:2021-05-02 16:58:17

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.

当你点击它的时候,它会改变文本的颜色,但是它展示了我的观点。

6 个解决方案

#1


47  

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对象”。

#2


37  

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');
});

#3


9  

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()

基本语法:$(选择).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(选择器)定义为“查询(或查找)”的美元符号在元素上执行的jQuery动作()

More on this

更多关于这

#4


6  

In jQuery, the $ sign it's just an alias to jQuery(), then an alias to a function.

在jQuery中,$符号只是jQuery的别名(),然后是函数的别名。

This page reports:

这一页报告:

Basic syntax is: $(selector).action()

基本语法:$(选择).action()

  • A dollar sign to define jQuery
  • 定义jQuery的美元符号
  • A (selector) to "query (or find)" HTML elements
  • (选择器)到“查询(或查找)”HTML元素
  • A jQuery action() to be performed on the element(s)
  • 要在元素上执行的jQuery动作()

#5


3  

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”)返回文本div的jQuery对象,然后可以对其进行修改。

#6


2  

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.$1 );

will alert "e"

会提醒“e”

But also here it's not "magic" but simply part of the properties name

但在这里,它并不是“魔法”,而是属性名的一部分

#1


47  

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对象”。

#2


37  

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');
});

#3


9  

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()

基本语法:$(选择).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(选择器)定义为“查询(或查找)”的美元符号在元素上执行的jQuery动作()

More on this

更多关于这

#4


6  

In jQuery, the $ sign it's just an alias to jQuery(), then an alias to a function.

在jQuery中,$符号只是jQuery的别名(),然后是函数的别名。

This page reports:

这一页报告:

Basic syntax is: $(selector).action()

基本语法:$(选择).action()

  • A dollar sign to define jQuery
  • 定义jQuery的美元符号
  • A (selector) to "query (or find)" HTML elements
  • (选择器)到“查询(或查找)”HTML元素
  • A jQuery action() to be performed on the element(s)
  • 要在元素上执行的jQuery动作()

#5


3  

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”)返回文本div的jQuery对象,然后可以对其进行修改。

#6


2  

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.$1 );

will alert "e"

会提醒“e”

But also here it's not "magic" but simply part of the properties name

但在这里,它并不是“魔法”,而是属性名的一部分