-1

试图解决prototype.js 和jQuery.js 之间的$ 冲突,因为我需要使用需要jQuery 库的jSlider。

我在 jquery.js 文件的末尾添加 jQuery.noConflict(),然后在 jslider.js 文件中我将 $._roundNumber 更改为 jQuery._roundNumber。然而我仍然得到一个错误,说 jQuery._roundNumber 不是一个函数。请帮忙。

4

2 回答 2

0

Fixed. Just realised that the reason jQuery.noConflict() was not working was because jQuery was already being used in the template page that I was including in my current page so the problem was two fold:

  1. '$()' which is the default jQuery selector was clashing with prototype.js which Tapestry includes by default

  2. when I added jQuery.noConflict() at the end of my jquery.new.js file then tried using 'jQuery()' as the selector it did not work either because a different version of jquery (let's call this jquery.old.js) was already included in the template page (which I was including in my current page). To get the jquery.old.js to work with prototype.js, the previous developer already used jQuery.noConflict() so the 'jQuery()' selector was already taken as well.

Fix: I added $j2 = jQuery.noConflict() at the end of jquery.new.js file then I used '$j2()' selector in my new library that uses the jSlider and I also modified the selector in the jSlider.js file. It all works well now.

I am leaving both jquery.new.js and jquery.old.js file in the project (rather than using just one) because the template page which uses the older jquery version is used all over the site (I don't want to test the entire site after changing this) while the new component I'm adding needs the new version of jquery (jquery.new.js) file. And yes I know it's better to add the no conflict commands at the top of my html page rather than at the end of the js library files - will change that after this update on stack overflow :)

于 2012-04-30T09:33:43.007 回答
0

这是集成原型和jquery的示例...可以检查您是否做对了。?

<script src="prototype.js"></script>
<script src="effects.js"></script>
<script src="jquery.js"></script>



jQuery.noConflict();
     jQuery(document).ready(function($){
         jQuery("a").click(function(){
            Effect.Shake('shake_demo');
        });
    });





    <div id="shake_demo" style="width:150px; height:40px; background:#ccc; text-align:center;">
        <a href="#"  style="line-height:40px;">Click me to shake!</a>
    </div>

现在,您可以使用以下代码四舍五入:

var txt=jQuery('#txtBox').val());
var amt = parseFloat(txt);
   alert(amt.toFixed(2);

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toFixed

于 2012-04-27T09:42:30.700 回答