【问题标题】:2 jQuery Scripts conflict !2 jQuery 脚本冲突!
【发布时间】:2011-07-04 15:02:29
【问题描述】:

我使用了两个 2 jquery(插件)脚本,一个用于导航,一个用于动态内容。

  1. http://www.i-marco.nl/weblog/jquery-accordion-3/
  2. http://css-tricks.com/dynamic-page-replacing-content/

他们单独工作很好,但不能一起工作。我厌倦了切换脚本,当我更改 index.html 中脚本的顺序时,最后一个脚本开始工作,就像它忽略了第一个脚本一样。例如,按此顺序,菜单正在工作:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.ba-hashchange.min.js" type="text/javascript"></script>
<script src="js/dynamicpage.js" type="text/javascript"></script>    
<script src="js/menu.js" type="text/javascript"></script>

按照这个顺序,动态内容正在工作:

<script src="js/menu.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.ba-hashchange.min.js" type="text/javascript"></script>
<script src="js/dynamicpage.js" type="text/javascript"></script>

这是动态页面.js:

$(function() {

    var newHash      = "",
        $mainContent = $("#main-content"),
        $pageWrap    = $("#wrapper"),
        baseHeight   = 0,
        $el;

    $pageWrap.height($pageWrap.height());
    baseHeight = $pageWrap.height() - $mainContent.height();

    $("nav").delegate("a", "click", function() {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $(window).bind('hashchange', function(){

        newHash = window.location.hash.substring(1);

        if (newHash) {
            $mainContent
                .find("#guts")
                .fadeOut(200, function() {
                    $mainContent.hide().load(newHash + " #guts", function() {
                        $mainContent.fadeIn(200, function() {
                            $pageWrap.animate({
                                height: baseHeight + $mainContent.height() + "px"
                            });
                        });
                        $("nav a").removeClass("current");
                        $("nav a[href="+newHash+"]").addClass("current");
                    });
                });
        };

    });

    $(window).trigger('hashchange');

})(jQuery);

这是 menu.js :

jQuery.fn.initMenu = function() {  
    return this.each(function(){

        var theMenu = $(this).get(0);
        $('.acitem', this).hide();
        $('li.expand > .acitem', this).show();
        $('li.expand > .acitem', this).prev().addClass('active');
        $('li a', this).click(
            function(e) {
                e.stopImmediatePropagation();
                var theElement = $(this).next();
                var parent = this.parentNode.parentNode;
                if($(parent).hasClass('noaccordion')) {
                    if(theElement[0] === undefined) {
                        window.location.href = this.href;
                    }
                    $(theElement).slideToggle('normal', function() {
                        if ($(this).is(':visible')) {
                            $(this).prev().addClass('active');
                        }
                        else {
                            $(this).prev().removeClass('active');
                        }    
                    });
                    return false;
                }
                else {
                    if(theElement.hasClass('acitem') && theElement.is(':visible')) {
                        if($(parent).hasClass('collapsible')) {
                            $('.acitem:visible', parent).first().slideUp('normal', 
                            function() {
                                $(this).prev().removeClass('active');
                            }
                        );
                        return false;  
                    }
                    return false;
                }
                if(theElement.hasClass('acitem') && !theElement.is(':visible')) {         
                    $('.acitem:visible', parent).first().slideUp('normal', function() {
                        $(this).prev().removeClass('active');
                    });
                    theElement.slideDown('normal', function() {
                        $(this).prev().addClass('active');
                    });
                    return false;
                }
            }
        }
    );
});
};

$(document).ready(function() {$('.menu').initMenu();});

请帮忙,我会很感激的!

附:这是当前脚本的顺序,其中只有动态内容有效,菜单无效:

<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/menu.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.ba-hashchange.min.js" type="text/javascript"></script>
<script src="js/dynamicpage.js" type="text/javascript"></script>

【问题讨论】:

  • 尝试联系他们的开发人员,也许他们以前已经遇到过这个问题。很难判断一个人是否不了解插件的完整代码或从未遇到过与您相同的情况。

标签: jquery scripting dynamic conflict


【解决方案1】:

在我看来,这两个脚本都在&lt;nav&gt;.menu(分别)内的&lt;a&gt; 标签上绑定了一个事件,并且它们都在点击函数()的末尾return false。 return false 将阻止事件传播并被其他脚本执行...

因此,实际上,您加载的最后一个脚本中的 click 函数在那些 a 标记上添加了点击绑定首先执行,返回 false 并因此阻止调用第一个脚本的绑定。

如果这是问题所在,修改插件以使点击函数返回 true 并调用 event.preventDefault() 可能会解决问题(这不是 100% 确定)

编辑

如果不深入研究这一切到底应该做什么,就很难修复,但请尝试在 dynamicpage.js 中替换它

$("nav").delegate("a", "click", function() {
        window.location.hash = $(this).attr("href");
        return false;
    });

有了这个

$("nav").delegate("a", "click", function(e) {
        window.location.hash = $(this).attr("href");
        e.preventDefault()
        return true;
    });

【讨论】:

  • 感谢您的回复!我尝试了更改,但它不起作用。我注意到另外一件事,当我在 IE 中打开时,它显示为空白,没有内容,没有菜单,只有背景图像。它只是一个简单的站点,左侧是菜单,右侧是内容。这个想法是让网站动态化,这意味着当有人点击菜单项时,内容应该在不刷新页面的情况下加载。为此,我使用了插件 dynamicpage.js,而对于菜单,我使用了 menu.js 插件。这两个插件单独工作正常,但不能一起工作。请在 yahoo dot com 的 kashife 给我发电子邮件,以便我可以将本地站点文件夹发送给您。
猜你喜欢
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
相关资源
最近更新 更多