【问题标题】:How to fix string saving as code to word?如何修复字符串保存为代码到单词?
【发布时间】:2020-10-23 22:10:53
【问题描述】:

我有以下代码:

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.

【问题讨论】:

  • text/plain 表示不解析 HTML,只是按字面意思显示。

标签: javascript html ms-word download save


【解决方案1】:

改变这一行:

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

到这里:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多