Possible Duplicate:
jQuery and MooTools Conflict
我在这里遇到了一些麻烦,如果尚未加载 jQuery 来运行我的脚本,我将加载它。我的脚本被插入到其他人网站的正文标记中的某个位置。 我的问题是,jquery 仅通过加载它(jquery)就与 mootools 发生冲突。
这是我的脚本:
var attemptCount=0;
if(typeof jQuery=='undefined') {
var script=document.createElement('script');
script.type='text/javascript';
script.src='https://ajax.googleapis.com/ajax/.../1.6.2/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
}
function init(){
if(arguments.callee.done) return;
if(typeof jQuery!='undefined'){
jQuery.noConflict();
arguments.callee.done=true;
// do some jquery stuff i.e. jQuery('...').etc...
}
}
function waitForJQuery(){
if(typeof jQuery!='undefined'){
init();
return;
}
if(attemptCount<100){
setTimeout(waitForJQuery,100);
}
return;
}
if(document.addEventListener){
document.addEventListener("DOMContentLoaded",init,false);
}
else if(document.attachEvent){
waitForJQuery();
}
window.onload=init;
任何帮助将不胜感激。
最佳答案
我相信 jQuery 需要在 MooTools 之前初始化,因为 $ 是一个别名,并且 jQuery 能够解除它的绑定(bind),如果考虑到您的情况,这不是太难的话,可能值得一试。
<p>jQuery sets this paragraph's color to red but MooTools sets the border color.</p>
<script type="text/javascript" src="jquery-1.x.x.js"></script>
<script type="text/javascript">
//no conflict jquery
jQuery.noConflict();
//jquery stuff
(function($) {
$('p').css('color','#ff0000');
})(jQuery);
</script>
<script type="text/javascript" src="moo1.x.js"></script>
<script type="text/javascript">
//moo stuff
window.addEvent('domready',function() {
$$('p').setStyle('border','1px solid #fc0');
});
</script>
关于javascript - Jquery Mootools 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8517300/