如何用 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');
}
}
对于标题,您可以执行以下操作:
$('title').html('my new meta title');
为您的元描述和关键字元素提供一个 id,以便您可以轻松选择元素:
$('#mdescription').attr('content', 'my new meta description');
$('#mdkeywords').attr('content', 'keyword one, keyword two');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)