2 个 jQuery 脚本冲突!

发布于 2024-10-19 19:33:01 字数 5268 浏览 1 评论 0原文

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

  1. http://www.i-marco.nl/weblog/jquery-accordion- 3/
  2. http://css-tricks.com/dynamic-page- Replacement-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>

这是dynamicpage.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();});

请帮忙,我将非常感激!

PS这是当前的脚本顺序,其中只有动态内容起作用,菜单不起作用:

<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>

I used two 2 jquery (plugins) scripts, one for navigation and one for dynamic content.

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

They work fine alone but not together. I tired to switch the scripts, when I change the order of scripts in index.html, the scripts which is in last starts working, like it is ignoring the first one. For example in this order the menu is working:

<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>

And in this order, the dynamic content is working:

<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>

This is dynamicpage.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);

And this is 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();});

Please help, I'll be really thankful !

P.S. this is the current order of scripts in which only dynamic content is working, menu is not working:

<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>

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

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

发布评论

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

评论(1

紫南 2024-10-26 19:33:01

在我看来,这两个脚本都在

因此,实际上,您加载的最后一个脚本中的单击函数将首先执行在这些 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;
    });

It looks to me that both scripts are binding an event on the <a> tags within <nav> and .menu (respectively), and both of them return false at the end of the click function(). The return false would prevent the event to propagate and be executed by the other script...

So in effect, the click function from last script you load that adds a click binding on those a tags gets executed first, return false and thus prevents the first script's binding to be called.

If that's the problem, modifying the plugins to have the click functions return true and calling event.preventDefault() instead might do the trick (not 100% sure on this one)

EDIT

It's hard to fix without digging into what exactly this is all supposed to do but try replacing this in dynamicpage.js

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

with this

$("nav").delegate("a", "click", function(e) {
        window.location.hash = $(this).attr("href");
        e.preventDefault()
        return true;
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文