You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用array.push()实现纯文本换行,解决文件下载HTML标签问题

Fixing Plain Text Newlines in Your Downloaded File

Looks like you're almost there! The issue with your current code is two-fold: you're not using the stripped text you created, and you need to convert those <br> tags into actual plain-text newlines before removing other HTML. Here's how to fix it:

Modified saver() Function

function saver() {
  // Get the raw HTML content from the command div
  var htmlContent = document.getElementById("command").innerHTML;
  
  // First, convert <br> tags (including self-closing <br/>) to plain text newlines
  var withNewlines = htmlContent.replace(/<br\s*\/?>/gi, '\n');
  
  // Remove any remaining HTML tags to get clean plain text
  var plainText = withNewlines.replace(/(<([^>]+)>)/ig, '');
  
  var filename = "File";
  var blob = new Blob([plainText], {type: "text/plain;charset=utf-8"});
  saveAs(blob, filename + ".txt");
}

What Changed?

  • Used your stripped text: Previously, you created newText but still passed the original HTML (text) to the Blob. Now we use the fully processed plain text.
  • Converted <br> to newlines: Before stripping all HTML, we replace <br> and <br/> with \n (the plain-text newline character). This ensures the line breaks you see on the page are preserved in the downloaded file.

Example Output

If your page shows:

GUID qwertyuiop
ID 12345

The downloaded file will now contain:

GUID qwertyuiop
ID 12345

This should give you the clean, properly formatted text file you're looking for!

内容的提问来源于stack exchange,提问作者AppleManYT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:34:25