2

我粘贴了所有代码,因为它可能与我要求的功能有关。我有帮助使我的一个功能运行。看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'] ));
    }
4

1 回答 1

2

可能的原因是您parseJSON做了额外的事情:$('#navigation ul').html(makeNav( new_json['navigation']));当您进行递归调用时,makeNav您不需要设置此 html 内容。嵌套定义makeNavinside的原因parseJSON可能是您不希望makeNav在范围之外可见,parseJSON因为您根本不会在此范围之外使用它,并且您不想污染“环境”。

无论如何,我真的不认为这是实现它的最佳方式......但这应该在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;
}
于 2013-01-18T08:06:06.830 回答