Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
2个jQuery脚本冲突!_Jquery_Scripting_Dynamic_Conflict - Fatal编程技术网

2个jQuery脚本冲突!

2个jQuery脚本冲突!,jquery,scripting,dynamic,conflict,Jquery,Scripting,Dynamic,Conflict,我使用了两个jquery(插件)脚本,一个用于导航,一个用于动态内容 他们单独工作很好,但不能一起工作。我很想切换脚本,当我在index.html中更改脚本的顺序时,最后一个脚本开始工作,就像它忽略了第一个一样。例如,菜单按此顺序工作: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> <sc

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

  • 他们单独工作很好,但不能一起工作。我很想切换脚本,当我在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>
    
    这是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>
    

    在我看来,这两个脚本都在
    .menu
    中的
    标记上绑定一个事件,并且它们都
    在单击函数()的末尾返回false
    。返回false将阻止事件传播并由其他脚本执行

    因此,实际上,在那些
    a
    标记上添加单击绑定的上一个脚本中的click函数首先执行,返回false,从而阻止调用第一个脚本的绑定

    如果这就是问题所在,那么修改插件使click函数返回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,我使用了menu.js插件。这两个插件单独工作,但不能一起工作。请给我发电子邮件到雅虎网站的kashife,这样我就可以给你发送本地网站文件夹了。
    $("nav").delegate("a", "click", function(e) {
            window.location.hash = $(this).attr("href");
            e.preventDefault()
            return true;
        });