問題描述

我使用 $(document).html(),但是丟擲一個錯誤… 有辦法獲得一切嗎?

最佳解決方案

你可以試試:

$("html").html();

如果您還要捕獲 hmtl 標籤,可以將它們連線到 html,如下所示:

function getPageHTML() {
  return "<html>" + $("html").html() + "</html>";
}

次佳解決方案

不要忘記<html> 標籤也可以有屬性。如果你想要整個檔案這個應該工作。

 $('html')[0].outerHTML

沒有 jQuery 也是微不足道的。

document.documentElement.outerHTML

如果你也想要 include the doctype,那就更多了。

var getDocTypeAsString = function () {
    var node = document.doctype;
    return node ? "<!DOCTYPE "
         + node.name
         + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
         + (!node.publicId && node.systemId ? ' SYSTEM' : '')
         + (node.systemId ? ' "' + node.systemId + '"' : '')
         + '>n' : '';
};

getDocTypeAsString() + document.documentElement.outerHTML

第三種解決方案

使用:

document.body.innerHTML

第四種方案

$("html").html()將獲得除外部最 html 標籤之外的所有東西。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇