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

odi*_*seh 11 javascript

$()功能在以下示例中执行了哪些操作?

function test(){
    var b=$('btn1');
    eval(b);
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*llo 45

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

值得注意的是,截至2009年12月,ECMAScript规范曾用于声明:

标识符中的任何位置都允许使用美元符号($)和下划线(_).美元符号仅用于机械生成的代码.(来源)

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


然而,最初的问题可能是指jQuery,Prototype等人中流行的DOM选择器.以下是一些jQuery示例:

$('*');         /* 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. */
Run Code Online (Sandbox Code Playgroud)

您可能需要查看以下文章以获取更多示例:


Luc*_*eis 23

这不是ECMAScript(JavaScript)的一部分.它只是由您的某些库定义的函数.通常是jQuery或PrototypeJS.