Javascript 从按钮将页面另存为html文件

Javascript 从按钮将页面另存为html文件,javascript,html,Javascript,Html,我一直在这些论坛上寻找一些可以按原样下载HTML页面的代码(即从其他按钮添加元素,基本上是我的div中的所有内容),以便下次可以在数据仍然存在的情况下下载。我一生都找不到任何只下载页面的东西。下面是我最接近实际保存html文档的地方,甚至(显然)它也只显示编写的文本。我可以编辑它使它封装整个页面吗? 由于经验不足,我只能道歉 function save() { var anchor = document.querySelectory('button'); anchor.setAttrib

我一直在这些论坛上寻找一些可以按原样下载HTML页面的代码(即从其他按钮添加元素,基本上是我的div中的所有内容),以便下次可以在数据仍然存在的情况下下载。我一生都找不到任何只下载页面的东西。下面是我最接近实际保存html文档的地方,甚至(显然)它也只显示编写的文本。我可以编辑它使它封装整个页面吗? 由于经验不足,我只能道歉

function save() {
  var anchor = document.querySelectory('button');
  anchor.setAttribute('download', 'index.html');
  anchor.setAttribute('href', 'data:text/html;charset=UTF-8,<p>asdf</p>')
}
函数保存(){
var anchor=document.queryselection('button');
setAttribute('download','index.html');
setAttribute('href','data:text/html;charset=UTF-8,asdf

')) }
一种解决方案是

按照该页面上的示例,使用
document.innerHTML
作为blob(我所使用的),实际上,我忘了这是否排除了标题。如果是这样的话,我找到了一个解决方案(也是在StackOverflow上),但我没有本地代码来了解我是如何解决这个问题的(如果我真的这样做了,我必须解决这个问题)

你可以试试这个

<a href ="#" id="donwload-link" onClick="myFunction()">download html content</a>

<script>
function myFunction() {
  var content = document.documentElement.innerHTML;
  download(content, "index", "txt")

}
function download(content, fileName, fileType) {
  var link = document.getElementById("donwload-link");
  var file = new Blob([content], {type: fileType});
  var donwloadFile = fileName + "." + fileType;
  link.href = URL.createObjectURL(file);
  link.download = donwloadFile
}
</script>

函数myFunction(){
var content=document.documentElement.innerHTML;
下载(内容,“索引”,“txt”)
}
函数下载(内容、文件名、文件类型){
var link=document.getElementById(“donwload link”);
var file=newblob([content],{type:fileType});
var donwloadFile=fileName+“+”文件类型;
link.href=URL.createObjectURL(文件);
link.download=donwloadFile
}

可能的重复: