我对.load()
and感到困惑$.ajax
。我的 jQuery 代码如下index.html
:
$('.load_ext').click(function(e) {
var url = $(this).attr("href");
parentContainer.append($(document.createElement("div")).load(url + ' #content_to_load').html('<p class="loading">Loading…</p>').hide().fadeIn('slow'));
})
和 HTML:
<a href="test.html" class="load_ext">test</a>
在上面的示例中,我从test.html
( id 的内容#content_to_load
)加载部分内容。我还想获取该页面的标题并将标题页test.html
替换为该index.html
页面。
我尝试做类似的事情:
var url = $(this).attr("href");
parentContainer.append($(document.createElement("div")).load(url + ' #content_to_load', function() {
console.log($(document).attr('title'));
}).html('<p class="loading">Loading…</p>').hide().fadeIn('slow'));
没有任何运气。它给了我当前页面的标题。如何通过执行以下操作替换标题:
$('title').text(newPageTitle);
谢谢!
编辑: 感谢@Jeff Schafer、@dystroy 和@zeroflagL,我设法解决了这个问题。这是更改后的代码:
var url = $(this).attr("href");
parentContainer.append($(document.createElement("div")).load(url + ' #content_to_load', function(responseText) {
var title = responseText.match(/<title>([^<]*)/)[1];
document.title = title;
}).html('<p class="loading">Loading…</p>').hide().fadeIn('slow'));