Prototype / Mootools conflict question

牧云@^-^@ 提交于 2019-12-09 00:03:07

问题


So I have a page that uses both Prototype and Mootools AJAX scripts.

There is much more Mootools that Prototype, so I'm wondering if Prototype has a function similar to jQuery's $j = jQuery.noConflict(); that I can use to redefine the $ alias for Prototype?

Thanks!


回答1:


The newest version of MooTools has a no conflict mode. Unfortunately, Prototype does not, which means that the $ will have to be bound to Prototype.

To enable the Dollar Safe Mode, upgrade your version of MooTools and make sure you include MooTools after Prototype.

<script type="text/javascript" src="prototype.js" />
<script type="text/javascript" src="mootools.js" />

After doing so, $ will be bound to Prototype. In MooTools scripts, replace all $ references to document.id.

// Before
var X = new Class({
    initialize: function(element){
        this.element = $(element);
    }
});


// After
var X = new Class({
    initialize: function(element){
        this.element = document.id(element);
    }
});

or you can use a closure:

(function(){

    var $ = document.id;

    this.X = new Class({
        initialize: function(element){
            this.element = $(element);
        }
    });

})();

More information about the Dollar Safe Mode is available in MooTools' blog:

http://mootools.net/blog/2009/06/22/the-dollar-safe-mode/




回答2:


I have a really simple solution:

<script src='mootools.js'></script>
<script>$moo = $; delete ($);</script>
<script src='prototype.js></script>



<script>

(function ($){


//here you can use $ of moo tools

})($moo);


//here you can use $ of prototype


</script>


来源:https://stackoverflow.com/questions/1722791/prototype-mootools-conflict-question

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