参见英文答案 >
What is jQuery(document) vs. $(document)6
这两者有什么区别?
这两者有什么区别?
- $('#SPANID').html("Some Text");
- jQuery('#SPANID').html("Some Text");
它是一些原型与jQuery吗?
解决方法
他们都做同样的事情.大多数库使用$作为访问库中函数的较短方式.
jQuery有很多方法来访问它的库:
- window.jQuery('#SPANID').html("Some Text");
- window.$('#SPANID').html("Some Text");
- jQuery('#SPANID').html("Some Text");
- $('#SPANID').html("Some Text");
如果您使用多个库,则可以使用jQuery或window.jQuery而不是$.
JQuery有一个名为jQuery.noConflict();的函数,它放弃了jQuery对$变量的控制,使$不能与jQuery一起工作.
这对使用多个使用$的库是有好处的.
所以当你使用jQuery时,你会做jQuery(‘#message’).addClassName(‘read’);和$(‘#message’).addClassName(‘read’);使用原型时.
(下一个位是一点点的话题,但如果你想使用$与多个库,将有所帮助)
虽然有一种方法可以在不同的库中同时使用$,使用匿名函数.像这样:
- (function($){
- })(jQuery);
- (function($){
- })(Prototype);
每个函数都传递库对象,所以jQuery和Prototype作为变量$允许使用它与许多库一起使用.如果您在每个库中包含每个库的代码,它将工作.
例如:
- (function($){
- $(document).ready(function(){
- $('#message').addClass('read');
- });
- })(jQuery);
- (function($){
- document.observe("dom:loaded",function() {
- $('message').addClassName('read');
- //Or
- $$('#message').addClassName('read');
- });
- })(Prototype);