我粘贴所有代码,因为它可能与我要的功能有关。我帮助我运行了其中一项功能。查看parseJSON()函数。为什么我必须使用2个函数(parseJSON()和嵌套的makeNav(navigation)),而不是仅使用一个parseJSON(navigation)(和ofc才能将内部元素从makeNav更改为parseJSON)。有人可以解释为什么它仅对我有用。因为我想了解它,而不仅仅是锻炼自己并继续前进。

    var new_json;
    $.get('navigation.json', function (json){
        new_json = json;
        parseJSON();

        var reload_page;
        var this_hash = window.location.hash;

        if( this_hash.length == 0 ){
            reload_page = "home";
        }else{
            reload_page = this_hash.replace('#', '');
        };

        loading(reload_page + '.html');
    });

    var cache = {};

    function loading(url){


        if( typeof(cache[url]) == 'undefined' ) {
            console.log( 'cache A does not exists' );

            container.load(url + ' .container>*', function(){

                cache[url] = container.html();
            });
        }else {
            console.log( 'cache A exists' );

            container.html(cache[url]);
        };
    };

    $('#navigation li a, #logo a').live('click',function(){

        var url = $(this).attr('href');

        window.location.hash = url.replace('.html', '');

        loading(url);

        return false;
    });




    function parseJSON() {

        function makeNav(navigation) {
            var nav_html = '';
            console.log( navigation.length );
            for (var i = 0; i < navigation.length; i++) {
                var name = navigation[i]['name'];
                var href = navigation[i]['href'];
                var submenu = navigation[i]['navigation'];

                nav_html += '<li><a href="' + href + '">' + name + '<span class="ddArrow"></span></a>';

                if( typeof(submenu) != 'undefined' ){
                    nav_html += '<ul>';
                    nav_html += makeNav(submenu);
                    nav_html += '</ul>';
                }
                nav_html += '</li>';
            }
            return nav_html;
        }
        $('#navigation ul').html(makeNav( new_json['navigation'] ));
    }

最佳答案

可能的原因是您的parseJSON会执行其他操作:$('#navigation ul').html(makeNav( new_json['navigation']));,并且当您递归调用makeNav时,无需设置此html内容。在makeNav内嵌套parseJSON的定义的原因可能是您不希望makeNavparseJSON范围之外可见,因为您根本没有在该范围以外使用它不想污染“环境”。

无论如何,我真的不认为这是实现它的最好方法……但是应该在https://codereview.stackexchange.com/上讨论。

要使用单个函数,如果没有嵌套的makeNav,则可以执行以下操作:

var new_json;
$.get('navigation.json', function (json){
    new_json = json;
    parseJSON();

    var reload_page;
    var this_hash = window.location.hash;

    if( this_hash.length == 0 ){
        reload_page = "home";
    }else{
        reload_page = this_hash.replace('#', '');
    };
    loading(reload_page + '.html');
});

var cache = {};

function loading(url){
    if( typeof(cache[url]) == 'undefined' ) {
        console.log( 'cache A does not exists' );
        container.load(url + ' .container>*', function(){
                cache[url] = container.html();
        });
    }else {
        console.log( 'cache A exists' );
        container.html(cache[url]);
    };
};

$('#navigation li a, #logo a').live('click',function(){
    var url = $(this).attr('href');
    window.location.hash = url.replace('.html', '');
    loading(url);
    return false;
});




function makeNav(navigation) {
    var nav_html = '';
    console.log( navigation.length );
    for (var i = 0; i < navigation.length; i++) {
        var name = navigation[i]['name'];
        var href = navigation[i]['href'];
        var submenu = navigation[i]['navigation'];

        nav_html += '<li><a href="' + href + '">' + name + '<span class="ddArrow"></span></a>';

        if( typeof(submenu) != 'undefined' ){
            nav_html += '<ul>';
            nav_html += makeNav(submenu);
            nav_html += '</ul>';
        }
        nav_html += '</li>';
    }
    return nav_html;
}

09-07 13:58