在jQuery中$是什么意思?

我最近遇到了一段代码。 它如下:

var myFeature = { 'config' : { 'container' : $('#myFeature') }, 'init' : function(config) { if (config && typeof(config) == 'object') { $.extend(myFeature.config, config); } myFeature.$container = myFeature.config.container; myFeature.$sections = myFeature.$container. find('ul.sections > li'); myFeature.$section_nav = $('
    '). attr('id','section_nav'). prependTo(myFeature.$container); myFeature.$item_nav = $('
      '). attr('id','item_nav'). insertAfter(myFeature.$section_nav); myFeature.$content = $('
      '). attr('id','content'). insertAfter(myFeature.$item_nav); myFeature.buildSectionNav(myFeature.$sections); myFeature.$section_nav.find('li:first').click(); myFeature.$container.find('ul.sections').hide(); myFeature.initialized = true; }, 'buildSectionNav' : function($sections) { $sections.each(function() { var $section = $(this); $('
    • '). text($section.find('h2:first').text()). appendTo(myFeature.$section_nav). data('section', $section). click(myFeature.showSection); }); }, 'buildItemNav' : function($items) { $items.each(function() { var $item = $(this); $('
    • '). text($item.find('h3:first').text()). appendTo(myFeature.$item_nav). data('item', $item). click(myFeature.showContentItem); }); }, 'showSection' : function() { var $li = $(this); myFeature.$item_nav.empty(); myFeature.$content.empty(); var $section = $li.data('section'); $li.addClass('current'). siblings().removeClass('current'); var $items = $section.find('ul li'); myFeature.buildItemNav($items); myFeature.$item_nav.find('li:first').click(); }, 'showContentItem' : function() { var $li = $(this); $li.addClass('current'). siblings().removeClass('current'); var $item = $li.data('item'); myFeature.$content.html($item.html()); } };

我知道$(’#myFeature’),$(这个)意味着什么。 但是$ li和myFeature。$ container是什么意思? 它们是某种变量吗? 如果是这样,myFeature的范围是什么。$ container? 因为它没有使用var声明,它是全局的吗?

这只是公共变量,$被授权成为var名称的一部分,而作者只想在开始时用$命名他的变量。 关于myFeature。$ container这只是myFeature对象的一个​​属性,所以它与myFeature的范围相同

$li$container只是变量名,命名如此,所以程序员知道它们是jQuery扩展对象。

不,它只是一个简单的变量名称。
我对包含jquery对象的变量做同样的事情,以便快速将它们与我的其他(非jquery)变量区分开来。

它只允许您从JavaScript变量中轻松识别jQuery变量。

例如:

 var $section = $li.data('section'); //jQuery variable var num = 2; //JavaScript variable 

如果你有很多带有JavaScript和jQuery变量的代码,那么会非常有用。

有关详细信息,请参见此处

使用像jQuery这样的框架时,程序员通常会在变量名前加上$符号,这样他就知道内容是一个jQuery对象。

因此,例如,当您绑定click事件并在函数内部时,您可以使用此变量。 但这指的是dom元素而不是jquery对象。

例如,您可以使用类似的东西来识别变量的值:

 var $this = $(this); $this.doSomeJquery(); 

美元符号($)是“JQuery”的别名

我的意思是

 $(document).ready(function(){ }); 

就像写:

 jQuery(document).ready(function(){ }); 

编辑:我想念解释这个问题,对不起。

是的,是变量名称

我要说它只是一些代码约定来表明它是一个包含Jquery对象(而不是DOM对象)的变量。

如果你看一下JQuery源代码(http://bit.ly/jqsource) – 最后,你会看到:

 // Expose jQuery to the global object window.jQuery = window.$ = jQuery; 

它只是对window.jQuery的引用。