所以我有一个同时使用 Prototype 和 Mootools AJAX 脚本的页面。
Prototype 还有更多 Mootools,所以我想知道 Prototype 是否具有类似于 jQuery 的功能$j = jQuery.noConflict();
,可以用来重新定义 Prototype 的 $ 别名?
谢谢!
所以我有一个同时使用 Prototype 和 Mootools AJAX 脚本的页面。
Prototype 还有更多 Mootools,所以我想知道 Prototype 是否具有类似于 jQuery 的功能$j = jQuery.noConflict();
,可以用来重新定义 Prototype 的 $ 别名?
谢谢!
最新版本的 MooTools 具有无冲突模式。不幸的是,Prototype 没有,这意味着$
will 必须绑定到 Prototype。
要启用 Dollar 安全模式,请升级您的 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 的博客中提供了有关 Dollar 安全模式的更多信息:
我有一个非常简单的解决方案:
<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>