替换整个body元素(连同其属性,如类和id)

时间:2016-06-02 17:00:32

标签: javascript jquery ajax jquery-load

我正在执行AJAX调用来替换我的网页内容。不幸的是,这保留了我的整个body元素及其旧类,这会弄乱我的页面样式。

我正在使用别人的代码,我并不完全理解:

function loadNewContent(url, bool) {
    url = ('' == url) ? window.location.pathname : url;
    var newSection = 'cd-'+url.replace('.html', '');
    var section = $('<div class="cd-main-content '+newSection+'"></div>');

    section.load(url+' .cd-main-content > *', function(event){
        // load new content and replace <main> content with the new one
        $('main').html(section);
        //if browser doesn't support CSS transitions - dont wait for the end of transitions
        var delay = ( transitionsSupported() ) ? 1200 : 0;
        setTimeout(function(){
            //wait for the end of the transition on the loading bar before revealing the new content
            ( section.hasClass('cd-about') ) ? $('body').addClass('cd-about') : $('body').removeClass('cd-about');
            $('body').removeClass('page-is-changing');
            $('.cd-loading-bar').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
                isAnimating = false;
                $('.cd-loading-bar').off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend');
            });

            if( !transitionsSupported() ) isAnimating = false;
        }, delay);

        if(url!=window.location && bool){
            //add the new page to the window.history
            //if the new page was triggered by a 'popstate' event, don't add it
            window.history.pushState({path: url},'',url);
        }
    });
}

是否可以修改这段代码,以便完全销毁旧的body元素并将其替换为新元素?或者至少用旧的class身体属性替换旧的 SELECT [user], [computer name], [last logon]='' -- LAST LOGON IS BLANK CURRENTLY FROM user_details u inner join server_details s on s. [Computer Name] = u.server where s.[SQL Server Edition] like 'dev%' -- Grabbing servers with 'developer' group by u.[user], s.[computer name] 身体属性?

1 个答案:

答案 0 :(得分:0)

您的代码似乎正在调用(并因此替换)“主要”代码。您网站的一部分,与正文不同。

$('main').html(section);

我要说你是否要将main替换为body,这应该会有所作为。