How to use $ for jQuery inside domready when prototype is using $ outside?

我的梦境 提交于 2019-12-22 17:40:41

问题


I'm unable to remove prototype from a JSF framework (RichFaces 3.3.3). And if I try noConflict and try to take over $ it breaks my application framework because its tightly coupled with prototype.

So is there a way that I can do this:

jQuery(function() {
    /*
        some code that within this domready function 
        allows me to use $() within this function
        and not interfere with $ being used for prototype
        outside?
    */
});

回答1:


Yes, it's already passed in as the first parameter to your ready handler, just use:

jQuery(function($) { 
  $("selector").doSomething();
});
//$ is still prototype here



回答2:


In general, you can write var $ = jQuery; to replace the $ symbol within a single function.

In your specific case, you can also use the first parameter of the callback.




回答3:


taking this link as a reference, you could do something like this:

jQuery(function($) { // like Nick Craver
});

and call the functions you need to have jQuery with:

var yourFunction = function(){
   var $ = this;
};

yourFunction.call(jQuery);

...

var yourFunction = (function($){

   return function() {

       // $ -> jQuery

   };

})(jQuery);

...

var yourFunction = (function(){

   var $ = this;

   return function() {

       // $ -> jQuery

   };

}).call(jQuery);



回答4:


The standard within my working group:

jQuery.noConflict();
(function ($) {
    //Do jQuery stuff using $ here.
})(jQuery);
//Do prototype stuff using $ here


来源:https://stackoverflow.com/questions/3882022/how-to-use-for-jquery-inside-domready-when-prototype-is-using-outside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!