0

我在单击的链接处加载页面的内容hrefusing .load(),如下所示:

$("html").on('click', 'a', function(event) {
    event.preventDefault(); // obviously
    var url = this.href + " #main"; // holds the content of #main at a given url
    $('#ajax-container').load(url); // loads that content into #ajax-container
    document.title = "?";
});

如何获取加载页面的标题并显示它?

4

1 回答 1

3

试试这个也许:

var url = this.href; //following techfoobar's comment

$('#ajax-container').load(url,function(data){
    document.title = $($.trim(data)).find('title').text();//$.trim() used for old IE to move unexpected characters
}); 

但是,你不会得到你期望的内容$('#ajax-container')

所以使用 $.get() 代替:

var url = this.href; //following techfoobar's comment

$.get(url,function(data){
     document.title = $('<div/>').html($.trim(data)).find('title').text();
     $('#ajax-container').html($('<div/>').html($.trim(data)).find('body').html());
});
于 2013-06-05T17:10:07.910 回答