【问题标题】:Prototype / Mootools conflict questionPrototype / Mootools 冲突问题
【发布时间】:2009-11-12 14:58:10
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: jquery mootools prototypejs conflict


    【解决方案1】:

    最新版本的 MooTools 具有无冲突模式。不幸的是,Prototype 没有,这意味着 $ 必须绑定到 Prototype。

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

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

    这样做之后,$ 将绑定到 Prototype。在 MooTools 脚本中,将所有 $ 引用替换为 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 的博客:

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

    【讨论】:

    • 谢谢,我希望 Prototype 有类似的东西,因为它是迄今为止规模较小的实现,但自然从来没有这么幸运。
    • 虽然我同意你不想要一个 200 kb(压缩)的库,但 MooTools 的 67 kb 压缩是非常可观的。不要将您的选择基于尺寸,而应基于功能。 MooTools 做了 Prototype 所做的一切,我建议放弃 Prototype。此外,使用该网站自定义构建 MooTools 并删除您不需要的任何内容。
    【解决方案2】:

    我有一个非常简单的解决方案:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多