最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Combining Scriptaculous and jQuery in a Rails application - Stack Overflow

programmeradmin0浏览0评论

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!

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!

Share Improve this question edited Mar 22, 2019 at 15:36 Shiladitya 12.2k17 gold badges28 silver badges42 bronze badges asked Sep 22, 2008 at 0:58 RichHRichH 6,1382 gold badges39 silver badges62 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15
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);

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.

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

发布评论

评论列表(0)

  1. 暂无评论