JQuery 与其他 JQuery 库冲突 [英] JQuery conflict with an other JQuery library

查看:51
本文介绍了JQuery 与其他 JQuery 库冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jquery 作为模块.我的 joomla 模板有一个集成的 jquery 菜单.所以他们互相冲突.

I use jquery for a module. My joomla template have an integrated jquery menu. So they conflict with each other.

有没有办法解决这个问题.跟随模块的脚本代码

Is there a way to solve this problem. Following the script code of the module

<script type="text/javascript" charset="utf-8">
    window.onload = function () {
        var container = jQuery('div.sliderGallery');
         var ul = jQuery('ul', container);

         var itemsWidth = ul.innerWidth() - container.outerWidth();

         jQuery('.slider', container).slider({
             min: 0,
             max: itemsWidth,
             handle: '.handle',
             stop: function (event, ui) {
                 ul.animate({'left' : ui.value * -1},340);
             },
             slide: function (event, ui) {
                 ul.css('left', ui.value * -1);
             }
         });
     };
</script>

推荐答案

解决问题需要做的是取消 jQuery 函数的别名并将其分配给另一个变量名(记住:变量可以是函数).您需要使用 jQuery.noConflict() 功能取消对 $() 函数的别名.这里有一个:

What you need to do to fix your problem is un-alias the jQuery function and assign it to another variable name (remember: variables can be functions). You need to use the jQuery.noConflict() function to un-alias the $() function. Here one one to do it:

// ...after all of Joomla's JS is done executing...

// before loading your version of jQuery var jquery = {}; // aka new Object()
jquery.joomla = jQuery.noConflict(); // moves jQuery into another namespace

// load your version

现在,当您加载您的版本时,它将接管 jQuery 和 $ 命名空间,但如果您需要,您仍然可以获得对 Joomla 的 jQuery 函数的其他引用.再重复一遍,基本流程是:

Now, when you load your version, it will take over the jQuery and $ namespaces, but you'll still have the other reference to Joomla's jQuery function if you need it. To re-iterate, the basic flow is:

  1. 加载 Joomla 的 jQuery
  2. 运行 Joomla 的 jQuery 相关代码
  3. 将 Joomla jQuery 移动到另一个命名空间
  4. 加载你的 jQuery
  5. 使用 $()
  6. 执行您的代码
  1. Load Joomla's jQuery
  2. Run Joomla's jQuery-dependent code
  3. Move Joomla jQuery into another namespace
  4. Load your jQuery
  5. Execute your code using $()

这篇关于JQuery 与其他 JQuery 库冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆