Jquery-Mootools 冲突

发布于 2024-12-04 00:38:35 字数 2000 浏览 0 评论 0原文

我一直在尝试使 2 个脚本(1 个 mootol 和 1 个 jquery)在同一页面上工作,但无济于事。我一直在研究许多论坛等,但我仍然无法使这两个脚本同时工作。

这就是它在我的标题中的样子:

<script type="text/javascript" src="<?php bloginfo('url'); ?>/wp-content/plugins/buddypress/bp-themes/bp-default/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript"> 
     jQuery.noConflict();
$(document).ready(function () { 
         var hide = false; 
     $("#posts-menu, .submenu").hover(function(){ 
         if (hide) clearTimeout(hide); 
         $(".submenu").fadeIn(); 
     }, function() { 
         hide = setTimeout(function() { 
             $(".submenu").fadeOut("slow"); 
         }, 250); 
     }); 

})(jQuery);
    </script>       
<script type="text/javascript" src="<?php bloginfo('url'); ?>/wp-content/plugins/buddypress/bp-themes/bp-default/js/mootools-core-1.3.2.js"></script>
<script type="text/javascript" src="<?php bloginfo('url'); ?>/wp-content/plugins/buddypress/bp-themes/bp-default/js/mootools-more-1.3.1.1.js"></script>
<script type="text/javascript" src="<?php bloginfo('url'); ?>/wp-content/plugins/buddypress/bp-themes/bp-default/js/wall.js"></script>
<script type="text/javascript" src="<?php bloginfo('url'); ?>/wp-content/plugins/buddypress/bp-themes/bp-default/js/wall-lide.js"></script>

<script type="text/javascript">
<!-- 

window.addEvent('domready',function(){


    // -- horizontal
    var myHorizontalSlide = new Fx.Slide('login-welcome-div-slide', {mode: 'vertical'});


    $('slideout').addEvent('click', function(event){
        event.stop();
        myHorizontalSlide.slideOut();
    });


});

// -->  
</script>

即使有 jquery.js 调用,我也管理 mootools 脚本来工作... 你可以看到我已经在第三行使用了 jQuery.noConflict(); 。

问题是,如果 jquery.noconflict 存在,那么 mootool 脚本可以工作,但 jquery 脚本不起作用!现在,如果删除 jquery.noconflict ,那么现在相反,jquery 脚本可以工作,但 mootool 脚本不能工作..我只是无法使它们中的两个工作!

任何形式的帮助将不胜感激..

i have been trying to make 2 scripts(1 mootol and 1 jquery) work at the same page to no avail..been researching many a forums and all but i still cant make the 2 work simultaneously.

this is how it looks in my header:

<script type="text/javascript" src="<?php bloginfo('url'); ?>/wp-content/plugins/buddypress/bp-themes/bp-default/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript"> 
     jQuery.noConflict();
$(document).ready(function () { 
         var hide = false; 
     $("#posts-menu, .submenu").hover(function(){ 
         if (hide) clearTimeout(hide); 
         $(".submenu").fadeIn(); 
     }, function() { 
         hide = setTimeout(function() { 
             $(".submenu").fadeOut("slow"); 
         }, 250); 
     }); 

})(jQuery);
    </script>       
<script type="text/javascript" src="<?php bloginfo('url'); ?>/wp-content/plugins/buddypress/bp-themes/bp-default/js/mootools-core-1.3.2.js"></script>
<script type="text/javascript" src="<?php bloginfo('url'); ?>/wp-content/plugins/buddypress/bp-themes/bp-default/js/mootools-more-1.3.1.1.js"></script>
<script type="text/javascript" src="<?php bloginfo('url'); ?>/wp-content/plugins/buddypress/bp-themes/bp-default/js/wall.js"></script>
<script type="text/javascript" src="<?php bloginfo('url'); ?>/wp-content/plugins/buddypress/bp-themes/bp-default/js/wall-lide.js"></script>

<script type="text/javascript">
<!-- 

window.addEvent('domready',function(){


    // -- horizontal
    var myHorizontalSlide = new Fx.Slide('login-welcome-div-slide', {mode: 'vertical'});


    $('slideout').addEvent('click', function(event){
        event.stop();
        myHorizontalSlide.slideOut();
    });


});

// -->  
</script>

i managed the mootools script to work even if theres a jquery.js call...
you can see that i already used jQuery.noConflict(); in the 3rd line..

the problem is that the mootool scripts works if the jquery.noconflict is there but the jquery scripts doesnt work! now if remove the jquery.noconflict its now the other way around, the jquery script works but not the mootool scripts..i just couldnt make the 2 of them work!

any kind of help would be greatly appreciated..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

瞎闹 2024-12-11 00:38:35

当您在 noConflict 模式下运行时,您无法使用 $ 便捷方法。您必须使用完整的 jQuery 形式。

jQuery.noConflict();
// Do something with jQuery
jQuery("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';

请避免使用 2 个 js 库,因为它会增加页面加载时间,进而产生 SEO 影响(负面影响)。

When you run in noConflict mode you can't use the $ convenience method. You have to use the full form jQuery instead.

jQuery.noConflict();
// Do something with jQuery
jQuery("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';

Please avoid the use of 2 js libraries as it will increase your page load time which in turn has SEO implications (negative ones).

权谋诡计 2024-12-11 00:38:35

问题是您没有将 jQuery 代码包含在匿名函数中。试试这个:

(function($){
    // inside here $ will always mean jQuery
    $(document).ready(function () { 
         var hide = false; 
         $("#posts-menu, .submenu").hover(function(){ 
             if (hide) clearTimeout(hide); 
             $(".submenu").fadeIn(); 
         }, function() { 
             hide = setTimeout(function() { 
                 $(".submenu").fadeOut("slow"); 
             }, 250); 
         }); 
    });
})(jQuery);

The problem is that you are not enclosing the jQuery code within an anonymous function. Try this:

(function($){
    // inside here $ will always mean jQuery
    $(document).ready(function () { 
         var hide = false; 
         $("#posts-menu, .submenu").hover(function(){ 
             if (hide) clearTimeout(hide); 
             $(".submenu").fadeIn(); 
         }, function() { 
             hide = setTimeout(function() { 
                 $(".submenu").fadeOut("slow"); 
             }, 250); 
         }); 
    });
})(jQuery);
乙白 2024-12-11 00:38:35

从 MooTools 的角度来看。

您将在 jQuery 之后添加 MooTools。那挺好的。因为(如果您不使用 jQuery.noConflict),MooTools 将看到 $ 已被其他人使用,并将保持原样。

您必须在 MooTools 脚本中将 $ 函数替换为 document.id() 。

jQuery 有这种无冲突模式。使用它时,您必须将 jQuery 中的 $ 函数替换为 jQuery()。

您可以替换出现的内容,也可以使用匿名函数在脚本中保留对 $ 的引用,并使其成为 MooTools 的 document.id() 或 jQuery 的 jQuery() 的别名。

(function($) {
    // $ will refer to MooTools' document.id() in this anonymous function
    // ...
})(document.id);

(function($) {
    // $ will refer to jQuery's jQuery() in this anonymous function
    // ...
})(jQuery);

From a MooTools point of view.

You're including MooTools after jQuery. That's good. Because (if you don't use jQuery.noConflict), MooTools will see $ is already used by someone else and will leave it as it is.

You will have to replace the $ function with document.id() in your MooTools scripts.

jQuery has this noConflict mode. When using it, you will have to replace the $ function from jQuery with jQuery().

Either you replace the occurrences or you can use anonymous functions to keep the references to $ in your scripts and make it an alias for MooTools' document.id() or jQuery's jQuery().

(function($) {
    // $ will refer to MooTools' document.id() in this anonymous function
    // ...
})(document.id);

(function($) {
    // $ will refer to jQuery's jQuery() in this anonymous function
    // ...
})(jQuery);
无风消散 2024-12-11 00:38:35

对于 jQuery 部分,使用 jQuery (区分大小写)而不是 $

Use jQuery (case-sensitive) instead of $ for the jQuery parts.

和影子一齐双人舞 2024-12-11 00:38:35

你正在破坏$

<script type="text/javascript">
  $.noConflict();
  jQuery(document).ready(function($) {
    // Code that uses jQuery's $ can follow here.
  });
  // Code that uses other library's $ can follow here.
</script>

You're clobbering the $.

<script type="text/javascript">
  $.noConflict();
  jQuery(document).ready(function($) {
    // Code that uses jQuery's $ can follow here.
  });
  // Code that uses other library's $ can follow here.
</script>
太阳哥哥 2024-12-11 00:38:35

您可以尝试在 MooTools 库之前包含 jQuery 库。

这个文档应该能更好地说明

You can try including the jQuery library before the MooTools library.

This document should illustrate better

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文