javascript - Jquery Mootools 冲突

标签 javascript jquery mootools onload

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>

来源:http://davidwalsh.name/jquery-mootools

关于javascript - Jquery Mootools 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8517300/

相关文章:

javascript - Twitter bootstrap 在小型设备上重新排序元素

android - Ajax 不能在使用 phonegap 的 Android 手机中工作

jquery - 以特殊格式对数据表中的数字进行排序

javascript - 我如何将此 jQuery 转换为 javascript?

javascript - Mootools "Cannot read property ' 调用“未定义”

javascript - 如何操作在运行时添加的 HTML 元素?

javascript - 无法使用 AngularJS (JavaScript) 设置边距

javascript - 替换数据属性中的部分 URL

javascript - 我们可以在悬停时使用相同的 jquery 功能两次,在单击时使用另一次吗?

javascript - 具有相同 'name' 属性的多个表单字段未发布