5

ドキュメントのメタ タイトルと説明を、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');
            }

}
4

1 に答える 1

9

タイトルについては、次のことができます。

$('title').html('my new meta title');

メタ ディスクリプションとキーワード要素に ID を付けて、要素を簡単に選択できるようにします。

$('#mdescription').attr('content', 'my new meta description');
$('#mdkeywords').attr('content', 'keyword one, keyword two');
于 2012-10-23T08:37:00.883 に答える