1

在我看来,这段代码应该克隆元素#loaded_start_container,并将克隆与其所有子元素一起放入#content-wrapper。

然而,这会创建一个克隆,但不会创建一个子级。如果我只是移动元素而不克隆,它可以工作。

var loaded_content = $('#loaded_start_container').clone().attr('id', 'content_start');
$('#content-wrapper').html(loaded_content);

jQuery 2.0.3

编辑。添加了整个Js

$(function(){
    var time_before_load = new Date().getTime();
    loadContent('start', function(){
        console.log('Now start IS loaded. Evidence ');
        console.log($('#loaded_start_container'));
        changeContent('start');
        loadContent('projects', function(){
            loadContent('about', function(){
                  console.log('All content has been loaded. It took '+ (new Date().getTime() - time_before_load) + ' miliseconds');
            });
        });
    });

    $('nav ul li').bind('click', function(){
        $('.active_content').removeClass('active_content');
        var clicked = $(this).addClass('active_content').attr('id');
        changeContent(clicked);
    });

});


changeContent = function(source) {
    console.log('Changed to ' + source);
    var loaded_content = $('#loaded_'+source+'_container').clone().attr('id', 'content_'+source);
    $('#content-wrapper').html(loaded_content);
};
loadContent = function(source, callback) {
    $('#loaded_'+source+'_container').load(source + '.html', callback());
};
4

1 回答 1

2

这将做到:

var loaded_content = $('#loaded_start_container').clone().attr('id', 'content_start');
$('#content-wrapper').append(loaded_content );

小提琴示例:

http://jsfiddle.net/88SLA/4/

.html()有效..但替换现有内容。看这里:

http://jsfiddle.net/88SLA/5/

于 2013-09-05T21:59:54.887 回答