如何用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
newTextbut 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
相关产品推荐
相关产品推荐

