簡體   English   中英

如何獲得標題的內部html

[英]how to get the inner html of the title

我試圖在不刷新頁面的情況下將我的頁面鏈接到另一個頁面並且我成功地做到了,但是第二頁的標題沒有改變它仍然是第一頁的標題。 第二頁有自己的標題標簽,其中包含頁面的標題,但是當我重定向它時它沒有反映,我該如何解決這個問題。

這是我的代碼:

let script = document.createElement("script");
script.src = "https://code.jquery.com/jquery-3.4.1.min.js";
script.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(script);

function goto(name_of_page) {

  // go get the page and paste it on the current page
  var request = new XMLHttpRequest();
  request.open("GET", name_of_page, true);
  request.onload = function() {
    if (request.status >= 200 && request.status < 400) {
      let resp = request.responseText;

  let link = name_of_page.split(".");
  let name = link[0];
  $("body").load(`${name_of_page}`);

  if (
    location.href.includes("localhost") ||
    location.href.includes("www")
  ) {
    window.history.pushState(name_of_page, "Title", name);
    console.log("has www or localhost");
  } else {
    window.history.pushState(name_of_page, "Title", name_of_page);
    console.log("does not have www");
  }

}
};
 request.send();

}

您可以將加載的數據解析為HTML 內容模板,因此一旦獲得數據:

/*...*/

let resp = request.responseText;

const tpl = document.createElement('template');
tpl.innerHTML = resp;

document.title = tpl.content.querySelector('title').textContent;

/*...*/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM