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

查看:81
本文介绍了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天全站免登陆