$()函数在JavaScript中做什么?

时间:2022-08-26 23:18:42

What does the $() function do in the following example?

下面的例子中$()函数做了什么?

function test(){
    var b=$('btn1');
    eval(b);
}

6 个解决方案

#1


21  

That's not part of ECMAScript (JavaScript). It's just a function defined by some library of yours. Usually jQuery or PrototypeJS.

这不是ECMAScript (JavaScript)的一部分。它只是你的某个库定义的一个函数。通常jQuery或PrototypeJS。

#2


37  

The $() method is not part of the JavaScript language. It is often defined in JavaScript frameworks such as jQuery and Prototype, as a DOM selector.

$()方法不是JavaScript语言的一部分。它通常在JavaScript框架(如jQuery和Prototype)中定义为DOM选择器。

It is interesting to note that up to until December 2009, the ECMAScript specification used to state:

有趣的是,直到2009年12月之前,ECMAScript规范都曾声明:

The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code. (Source)

美元符号($)和下划线(_)在标识符中任何地方都是允许的。美元符号只用于机械生成的代码。(源)

However this "dollar sign for mechanically generated code" hint was removed from the current ECMAScript specification (ECMA 262 - 5th Edition / December 2009).

然而,这个“机械生成代码的美元符号”提示从当前的ECMAScript规范(ECMA 262 -第5版/ 2009年12月)中删除了。


Nevertheless, the original question was probably referring to the popular DOM selectors in jQuery, Prototype, et al. Here are a few jQuery examples:

尽管如此,最初的问题可能指的是jQuery、Prototype等流行的DOM选择器。

$('*');         /* This selector is a wild card method and will select all 
                   elements in a document. */

$('#id');       /* This selector selects an element with the given ID. */

$('.class');    /* The class selector will gather all elements in the 
                   document with the given class name. */

$('element');   /* This selector will collect all elements in a document with 
                   the given tag name i.e. table, ul, li, a etc. */

You may want to check the following article for more examples:

您可能想要查看下面的文章以获得更多的示例:

#3


1  

I think you're dealing with a framework here. Most frameworks include $ functions to generate custom objects from a selector or dom object.

我认为你在处理一个框架。大多数框架包含从选择器或dom对象生成自定义对象的$函数。

#4


0  

Answering to your question, this function return the DOM object with the specified ID.

回答您的问题,这个函数返回具有指定ID的DOM对象。

For example, if you have on your HTML:

例如,如果你有你的HTML:


<div id="thisIsMyDivId">This is some content</div>

You can get the DIV element using:

您可以使用以下命令获取DIV元素:


var myDiv = $('thisIsMyDivId');

The idea of this function is to replace the necessity of use document.getElementById to do this.

该功能的思想是取代使用文档的必要性。getElementById来做到这一点。

And......repeating what everyone here already did...It is not a native JS function, it is implemented on some Frameworks (Prototype and jQuery AFAIK).

和……重复这里每个人已经做过的事情……它不是一个本地的JS函数,而是在一些框架(Prototype和jQuery AFAIK)上实现的。

#5


-1  

Its not JS built in function, its Prototype
http://www.prototypejs.org/api/utility/dollar-dollar

它的原型是http://www.prototypejs.org/api/utility/dollar

#6


-1  

$ sign is not part of javascript it is a part of a javascript framework probably jQuery

$ sign不是javascript的一部分,它可能是javascript框架的一部分

More details have a look at this article

更多细节请看这篇文章。

#1


21  

That's not part of ECMAScript (JavaScript). It's just a function defined by some library of yours. Usually jQuery or PrototypeJS.

这不是ECMAScript (JavaScript)的一部分。它只是你的某个库定义的一个函数。通常jQuery或PrototypeJS。

#2


37  

The $() method is not part of the JavaScript language. It is often defined in JavaScript frameworks such as jQuery and Prototype, as a DOM selector.

$()方法不是JavaScript语言的一部分。它通常在JavaScript框架(如jQuery和Prototype)中定义为DOM选择器。

It is interesting to note that up to until December 2009, the ECMAScript specification used to state:

有趣的是,直到2009年12月之前,ECMAScript规范都曾声明:

The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code. (Source)

美元符号($)和下划线(_)在标识符中任何地方都是允许的。美元符号只用于机械生成的代码。(源)

However this "dollar sign for mechanically generated code" hint was removed from the current ECMAScript specification (ECMA 262 - 5th Edition / December 2009).

然而,这个“机械生成代码的美元符号”提示从当前的ECMAScript规范(ECMA 262 -第5版/ 2009年12月)中删除了。


Nevertheless, the original question was probably referring to the popular DOM selectors in jQuery, Prototype, et al. Here are a few jQuery examples:

尽管如此,最初的问题可能指的是jQuery、Prototype等流行的DOM选择器。

$('*');         /* This selector is a wild card method and will select all 
                   elements in a document. */

$('#id');       /* This selector selects an element with the given ID. */

$('.class');    /* The class selector will gather all elements in the 
                   document with the given class name. */

$('element');   /* This selector will collect all elements in a document with 
                   the given tag name i.e. table, ul, li, a etc. */

You may want to check the following article for more examples:

您可能想要查看下面的文章以获得更多的示例:

#3


1  

I think you're dealing with a framework here. Most frameworks include $ functions to generate custom objects from a selector or dom object.

我认为你在处理一个框架。大多数框架包含从选择器或dom对象生成自定义对象的$函数。

#4


0  

Answering to your question, this function return the DOM object with the specified ID.

回答您的问题,这个函数返回具有指定ID的DOM对象。

For example, if you have on your HTML:

例如,如果你有你的HTML:


<div id="thisIsMyDivId">This is some content</div>

You can get the DIV element using:

您可以使用以下命令获取DIV元素:


var myDiv = $('thisIsMyDivId');

The idea of this function is to replace the necessity of use document.getElementById to do this.

该功能的思想是取代使用文档的必要性。getElementById来做到这一点。

And......repeating what everyone here already did...It is not a native JS function, it is implemented on some Frameworks (Prototype and jQuery AFAIK).

和……重复这里每个人已经做过的事情……它不是一个本地的JS函数,而是在一些框架(Prototype和jQuery AFAIK)上实现的。

#5


-1  

Its not JS built in function, its Prototype
http://www.prototypejs.org/api/utility/dollar-dollar

它的原型是http://www.prototypejs.org/api/utility/dollar

#6


-1  

$ sign is not part of javascript it is a part of a javascript framework probably jQuery

$ sign不是javascript的一部分,它可能是javascript框架的一部分

More details have a look at this article

更多细节请看这篇文章。