我對和$.ajax
感到困惑。我在我的index.html
以下jQuery代碼:如何更改.load()頁面的標題,並在jQuery中加載新頁面的標題
$('.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:從test.html
(標識#content_to_load
的內容)
<a href="test.html" class="load_ext">test</a>
在上面的例子中,我加載部分內容。我也想抓住那個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謝弗,@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'));
http://stackoverflow.com/questions/2115200/jquery-load-or-ajax-to-get-and-set-page-title – Dom 2013-03-13 14:37:43
@Dom我試過,但他們使用'#title'編號來找到標題。我想要頁面的'
'嘗試在該.load.的回調中設置新的document.title。選定的答案。 – Dom 2013-03-13 14:39:50