如何替换ajax加载的html文档中的元标题,关键字和描述?

DJE*_*ock 5 ajax jquery replace title meta-tags

如何用ajax调用中传入的html文档中的元标题和描述替换文档上的元标题和描述?

我在相邻的html文档中调用#content div中的新内容.我想要发生的是当新内容加载时我想要替换主要文档标题(以及描述和关键字......可能因为我正在使用的工具)以及正在加载的html文件中的元标题.

我考虑过使用replace(); 或匹配(); 但我只是想找出最好的方法.

这是脚本:

// JavaScript Document
$(document).ready(function() {

    var toLoad
    $(window).bind( "hashchange", function(e) {
            loadcontent();
            return false;

    });



        $('#toc li a').click(function(){
            window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-0);
            var href = $(this).attr( "href" );
            $('meta[name=title]').attr('title', new_title);
            $('meta[name=description]').attr('description', new_description);
            $('meta[name=keywords]').attr('keywords', new_keywords);
            //alert("hey" + window.location.hash);
            $('#breadcrumbs h1').append(" </a><a href='index.html"+ window.location.hash + "' >" + $(this).attr('href') +" ></a>");
            $.bbq.pushState({ url: href });
            $(window).trigger( "hashchange" );

        });

    loadcontent();

});

function loadcontent(){

    var toLoad = window.location.hash.replace("#","") +'.html #content';
            $('#content').hide('slow');
            $('#load').remove();
            $('#conContainer').append('<span id="load">LOADING...</span>');
            $('#load').fadeIn('normal');

            $('#content').load(toLoad,'',function(returnText,status,request){
                showNewContent()
            });

            function loadContent() {
                $('#content').load(toLoad,'',showNewContent());

            }
            function showNewContent() {
                $('#content').show('slow',hideLoader());
            }
            function hideLoader() {
                $('#load').fadeOut('normal');
            }

}
Run Code Online (Sandbox Code Playgroud)

Fra*_*key 9

对于标题,您可以执行以下操作:

$('title').html('my new meta title');
Run Code Online (Sandbox Code Playgroud)

将元描述和关键字元素设为id,以便轻松选择元素:

$('#mdescription').attr('content', 'my new meta description');
$('#mdkeywords').attr('content', 'keyword one, keyword two');
Run Code Online (Sandbox Code Playgroud)