简体   繁体   English

在JQuery中,美元符号和美元符号后跟一段时间有什么区别?

[英]What is the difference between a dollar sign and a dollar sign followed by a period in JQuery?

In jQuery, what is the difference between $ and $. 在jQuery中, $$.之间有什么区别$. ? Please provide a practical explanation if possible. 如果可能,请提供实用的解释。

$ is a reference (or synonym, if you want an analogy) to the global jQuery object. $是全局jQuery对象的引用(或同义词,如果你想要一个类比)。 $.<method> calls a static method of the jQuery object, where $(<selector>) creates a new instance of the jQuery object. $.<method>调用jQuery对象的静态方法,其中$(<selector>)创建jQuery对象的新实例。

In the context of jQuery, $ refers to the global jQuery object . 在jQuery的上下文中, $指的是全局jQuery 对象

$. by itself is invalid JavaScript. 本身就是无效的JavaScript。 As $ is an object , methods on this object are called like on any other object: $.methodname() . 由于$是一个对象 ,因此在任何其他对象上调用此对象上的方法: $.methodname()

Maybe it becomes clearer by substituting $ with jQuery : jQuery.methodname() . 也许通过用jQuery代替$来变得更清楚: jQuery.methodname()

I presume you are asking about the syntactic difference between $('#selector'); 我认为你在询问$('#selector');之间的句法差异$('#selector'); and $.parseJSON(str); $.parseJSON(str); .

The former is an invocation of a function called $ and the latter invokes it's method called parseJSON . 前者是一个名为$的函数的调用,后者调用它的方法叫做parseJSON Yup, functions can have methods - it is possible because Javascript functions are also objects. 是的,函数可以有方法 - 这是可能的,因为Javascript函数也是对象。 Figuratively speaking: 形象地说:

<script type="text/javascript">

function $(str) {
   alert(str);
}

$.parseJSON = function () {
   alert('Parsing JSON');
}

$('Hi');
$.parseJSON('{}');


</script>

$ - dollar sign is also same as Jquery usage. $ - 美元符号也与Jquery使用相同。 But I preferred to use dollar sign because I like the way dollar sign is. 但我更喜欢使用美元符号,因为我喜欢美元符号的方式。

From http://en.wikipedia.org/wiki/JQuery : 来自http://en.wikipedia.org/wiki/JQuery

the $ function, which is a factory method for the jQuery object. $ function,它是jQuery对象的工厂方法。 These functions, often called commands, are chainable; 这些函数通常称为命令,是可链接的; they each return a jQuery object 他们每个都返回一个jQuery对象

$.-prefixed functions. $ .-前缀函数。 These are utility functions which do not work on the jQuery object per se. 这些是实用程序函数,它们本身不适用于jQuery对象。

Typically, access to and manipulation of multiple DOM nodes begins with the $ function being called with a CSS selector string, which results in a jQuery object referencing matching elements in the HTML page. 通常,对多个DOM节点的访问和操作始于使用CSS选择器字符串调用$ function,这导致jQuery对象引用HTML页面中的匹配元素。 This node set can be manipulated by calling instance methods on the jQuery object, or on the nodes themselves. 可以通过调用jQuery对象上的实例方法或节点本身来操纵此节点集。 For example: 例如:

 $("div.test").add("p.quote").addClass("blue").slideDown("slow"); 

This line finds the union of all div tags with class attribute test and all p tags with CSS class attribute quote, adds the class attribute blue to each matched element, and then slides them down with an animation. 这一行找到所有div标签与class属性test的联合,所有p标签都带有CSS class属性quote,将class属性blue添加到每个匹配的元素,然后用动画向下滑动它们。 The $ and add functions affect the matched set, while the addClass and slideDown affect the referenced nodes. $和add函数会影响匹配的集合,而addClass和slideDown会影响引用的节点。

The methods prefixed with $. 前缀为$的方法。 are convenience methods or affect global properties and behaviour. 是便利方法或影响全局属性和行为。 For example, the following is an example of the map function called each in jQuery: 例如,以下是jQuery中调用的map函数的示例:

 $.each([1,2,3], function(){ document.write(this + 1); }); 

This writes the number 234 to the document. 这将数字234写入文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM