问题
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