2 回答

TA貢獻1804條經驗 獲得超7個贊
我不確定是什么打破了第三代克隆,因此它導致js信息輸出到頁面,但是使用實際文檔對象來克隆/操作原始文件并輸出其內容可能會更好Blob對象的字符串。例如,我使用您的基本saveFile.html進行了測試,并進行了以下更改:
//remove original clone var and replace with:
var clone = document.cloneNode(true);
// grab textarea elements from both original document and clone:
var doc_input = document.getElementsByTagName("textarea")[0];
var clone_input = clone.getElementsByTagName("textarea")[0];
// set clone textarea's innerHTML to current textarea value:
clone_input.innerHTML = doc_input.value;
// use outerHTML of clone.documentElement to get string for Blob
var clone_string = [clone.documentElement.outerHTML];
var file = new Blob([clone_string], {"type":"text/html"});
我看到的唯一缺點是:
這可能很難擴展到更通用的框架,用于生成當前加載的HTML頁面狀態的“實時HTML文件”(盡管它不應該比您的示例方法更復雜)。
返回的字符串clone.documentElement.outerHTML似乎將文檔類型聲明放到一個簡單元素中,以便:
不在輸出字符串中。你可以使用類似的東西:
var clone_string = ["<!doctype html>" + clone.documentElement.outerHTML];
作為一種解決方法?;蛘?,對于更強大的東西:
var doc_doctype = new XMLSerializer().serializeToString(document.doctype);
var clone_string = [doc_doctype + clone.documentElement.outerHTML];
添加回答
舉報