【发布时间】:2009-11-12 14:58:10
【问题描述】:
所以我有一个同时使用 Prototype 和 Mootools AJAX 脚本的页面。
Mootools 比 Prototype 还多,所以我想知道 Prototype 是否有类似于 jQuery 的 $j = jQuery.noConflict(); 的功能,可以用来重新定义 Prototype 的 $ 别名?
谢谢!
【问题讨论】:
标签: jquery mootools prototypejs conflict
所以我有一个同时使用 Prototype 和 Mootools AJAX 脚本的页面。
Mootools 比 Prototype 还多,所以我想知道 Prototype 是否有类似于 jQuery 的 $j = jQuery.noConflict(); 的功能,可以用来重新定义 Prototype 的 $ 别名?
谢谢!
【问题讨论】:
标签: jquery mootools prototypejs conflict
最新版本的 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 的博客:
【讨论】:
我有一个非常简单的解决方案:
<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>
【讨论】: