JavaScript 中的 $() 函数有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2167544/
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 $() function do in JavaScript?
提问by odiseh
What does the $()function do in the following example?
$()在下面的例子中,函数做了什么?
function test(){
var b=$('btn1');
eval(b);
}
回答by Daniel Vassallo
The $()method is not part of the JavaScript language. It is often defined in JavaScript frameworks such as jQueryand Prototype, as a DOMselector.
该$()方法不是 JavaScript 语言的一部分。它通常在jQuery和Prototype等 JavaScript 框架中定义为DOM选择器。
It is interesting to note that up to until December 2009, the ECMAScriptspecification 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 选择器。以下是一些 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. */
You may want to check the following article for more examples:
您可能需要查看以下文章以获取更多示例:
回答by Luca Matteis
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。
回答by Pepijn
I think you're dealing with a framework here. Most frameworks include $ functions to generate custom objects from a selector or dom object.
我认为你在这里处理一个框架。大多数框架都包含 $ 函数来从选择器或 dom 对象生成自定义对象。
回答by vfn
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.
这个函数的想法是代替使用 document.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)上实现的。
回答by Adir
Its not JS built in function, its Prototype
http://www.prototypejs.org/api/utility/dollar-dollar
它不是 JS 内置函数,它的原型
http://www.prototypejs.org/api/utility/dollar-dollar

