原型/Mootools冲突问题 [英] Prototype / Mootools conflict question

查看:81
本文介绍了原型/Mootools冲突问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个同时使用Prototype和Mootools AJAX脚本的页面.

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

该原型还有更多的Mootools,所以我想知道原型是否具有类似于jQuery $j = jQuery.noConflict();的功能,可以用来为原型重新定义$别名吗?

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?

谢谢!

推荐答案

MooTools的最新版本具有无冲突模式.不幸的是,Prototype并非如此,这意味着$将必须绑定到Prototype.

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.

要启用美元安全模式,请升级您的MooTools版本,并确保在原型之后包含MooTools.

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" />

这样做之后,$将绑定到Prototype.在MooTools脚本中,将所有$引用替换为document.id.

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);
    }
});

或者您可以使用闭包:

(function(){

    var $ = document.id;

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

})();

有关美元安全模式"的更多信息,请参见MooTools的博客:

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

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

这篇关于原型/Mootools冲突问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆