使用加载网站的部分内容是很常见的
$('#where').load('page.php #what');
但是如何根据page.php标题更新当前文档的标题呢?
我需要在 JavaScript 中执行此操作:
$url = "http://www.mywebiste.com/page.php";
$file = file_get_contents($url);
if(preg_match("/<title>(.+)<\/title>/i",$file,$result)
print "The title of $url is <b>$result[1]</b>";
else
print "The page doesn't have a title tag";
这可能吗?
最佳答案
尝试获取
$.get('page.php', function(data){
var the_page_loaded = $(data);
document.title = $('title', the_page_loaded).text();
$('#where').html($('#what', the_page_loaded)); //do the load as you wanted
});
关于javascript - 从同一域中的 URL 获取标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8493400/