如何将保存为代码的字符串修复为单词?

How to fix string saving as code to word?

我有以下代码:

function downloadNotes() {
  var information = document.getElementById("text").innerHTML;
  var textToBLOB = new Blob([information], { type: 'text/plain' });
  var sFileName = 'formData = document.doc';

  var newLink = document.createElement("a");

     newLink.download = sFileName;

 if (window.webkitURL != null) {
     newLink.href = window.webkitURL.createObjectURL(textToBLOB);
 }
 else {
     newLink.href = window.URL.createObjectURL(textToBLOB);
     newLink.style.display = "none";
     document.body.appendChild(newLink);
 }

     newLink.click();
}

当我保存我的笔记时,它成功地将它保存到word中,但是当我打开它时,它显示的是全部压缩的代码而不是字符串输出: Here.

更改此行:

var information = document.getElementById("text").innerHTML;

为此:

var information = document.getElementById("text").innerText;

您的代码正在读取元素的 HTML 内容,而不是元素的文本值。如果这不起作用,您可能需要对其进行编码以删除 HTML.