jQuery语法-何时使用$(美元)vs jQuery [英] jQuery syntax - when to use $ (dollar) vs jQuery

查看:81
本文介绍了jQuery语法-何时使用$(美元)vs jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两者之间有什么区别?

What's difference between this two?

$('#SPANID').html("Some Text");

jQuery('#SPANID').html("Some Text");

是jQuery还是原型?

Is it something prototype vs jQuery?

推荐答案

它们都做同样的事情.大多数库使用$作为访问库中函数的捷径.

They both do the same thing. Most Libraries use $ as a shorter way to access functions within the libraries.

jQuery有许多访问其库的方法:

jQuery has many ways of accessing its library:

window.jQuery('#SPANID').html("Some Text");

window.$('#SPANID').html("Some Text");

jQuery('#SPANID').html("Some Text");

$('#SPANID').html("Some Text");

jQuery或window.如果您使用多个库,则可以使用jQuery代替$.

jQuery or window.jQuery can be used instead of $ if you were using more than one library.

JQuery具有一个名为 jQuery.noConflict(); 的函数,该函数放弃了jQuery对$变量使$不适用于jQuery.

JQuery has a function called jQuery.noConflict(); which relinquishs jQuery's control of the $ variable making $ not work with jQuery.

这对于使用多个使用$的库将是一个好选择.

This would be good for using more than one library that use $.

因此,当您使用jQuery时,使用原型时将执行jQuery('#message').addClassName('read');$('#message').addClassName('read');.

So you when you use jQuery you would do jQuery('#message').addClassName('read'); and $('#message').addClassName('read'); when using Prototype.

(接下来的内容有些偏离主题,但是如果您想将$与多个库一起使用,会有所帮助)

(This next bit is a little off topic but will help if you want to use $ with multiple libraries)

尽管有一种方法可以使用匿名函数同时在不同的库上使用$.像这样:

Although there is a way to use $ on different libraries at the same time, using anonymous functions. like so:

(function($){


})(jQuery);


(function($){


})(Prototype);

每个函数都传递库对象,因此jQuery和Prototype作为变量$允许与许多库一起使用.如果您在每个库中包含每个库的代码,它将起作用.

Each of the functions passes the library object, so jQuery and Prototype, as the variable $ allowing use to use it with many libraries. If you contain your code for each library within each one it will work.

例如:

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

         $('#message').addClass('read');

    });

})(jQuery);


(function($){
     document.observe("dom:loaded", function() {

         $('message').addClassName('read');
         //Or
         $$('#message').addClassName('read');

     });

})(Prototype);

这篇关于jQuery语法-何时使用$(美元)vs jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆