rails jquery and prototype

之前的项目用的是prototype  ,许多功能是用prototype实现的
现在项目要使用jquery,就必须混用
也可以只用jquery


There is a problem with using them together though: Prototype came up with this nice function called $() which is slightly more than a shortcut to document.getElementById(). This function is the main function in Prototype. Well,  John Resig , the creator of jQuery, also used to be a Prototype fan, so the main method in jQuery is also called $(). The jQuery website says that all you have to do to get them to play nice is to include jQuery after you include Prototype. Unfortunately, this is not enough. There is just a little bit more you have to do:

In the jQuery core code (which includes ajax, animations, et al.) there is a method called jQuery(). The $() method is aliased for the jQuery() in the code. This means that all you have to do is remove the part of jQuery that alias the $() method. In jQuery 1.0.4 uncompressed the lines you need to comment out start at line 60:

Change:

// Map over the $ in case of overwrite
if ( typeof $ != "undefined" )
jQuery._$ = $;

// Map the jQuery namespace to the '$' one
var $ = jQuery;

To:


// Map over the $ in case of overwrite
//if ( typeof $ != "undefined" )
//jQuery._$ = $;

// Map the jQuery namespace to the '$' one
//var $ = jQuery;

Once this is done, jQuery will no longer interfere with Prototype. You will have to use the jQuery() method to create a jQuery object, unless you alias the method to something else. I alias it to jq like this:

yessss

Change:

var $ = jQuery;

To:

var jq = jQuery;

One last thing:   Although all of the core uses JQuery() internally, there is no guaranty that any plugin will do the same, in my experience none do. The solution that works for me is to do a find/replace in the downloaded plugin, replacing any $( with jQuery(.

That's it. Now you can easily use jQuery and prototype in your rails app, even in the same view. I regularly use inline edit (which produces Prototype code) and jQuery in the same view, no problems.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值