Combining Scriptaculous and jQuery in a Rails application

核能气质少年 提交于 2019-12-06 05:12:03

问题


I've got the following situation

  • A rails application that makes use of rjs / Scriptaculous to offer AJAX functionality
  • Lot of nice javascript written using jQuery (for a separate application)

I want to combine the two and use my jQuery based functionality in my Rails application, but I'm worried about jQuery and Scriptaculous clashing (they both define the $() function, etc).

What is my easiest option to bring the two together? Thanks!


回答1:


jQuery.noConflict();

Then use jQuery instead of $ to refer to jQuery. e.g.,

jQuery('div.foo').doSomething()

If you need to adapt jQuery code that uses $, you can surround it with this:

(function($) {
...your code here...
})(jQuery);



回答2:


I believe it's jQuery.noConflict().

You can call it standalone like this:

jQuery.noConflict();

jQuery('div').hide();

Or you can assign it to another variable of your choosing:

var $j = jQuery.noConflict();

$j('div').hide();

Or you can keep using jQuery's $ function inside a block like this:

jQuery.noConflict();

 // Put all your code in your document ready area
 jQuery(document).ready(function($){
   // Do jQuery stuff using $
   $("div").hide();
 });
// Use Prototype with $(...), etc.
$('someid').hide();

For more information, see Using jQuery with Other Libraries in the jQuery documentation.




回答3:


jRails is a drop-in replacement for scriptaculous/prototype in Rails using the jQuery library, it does exactly what you're looking for.



来源:https://stackoverflow.com/questions/112721/combining-scriptaculous-and-jquery-in-a-rails-application

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