我在页面上有一个DIV,我希望用户能够将其作为文本文件下载(这是一个许可证密钥)。该DIV具有\n
符号,使用white-space: pre;
正确显示。我使用下面的代码创建带有DIV内容的临时<a>
链接并下载。这在Chrome中很好,但是FireFox松开了新的行符号,我得到了单行文本。
downloadLicenseBtn = $("#download_license");
downloadLicenseBtn.click(function(e){
e.preventDefault();
var $temp = $("<a>");
$(body).append($temp);
$temp.attr({
download: 'license.txt',
href: "data:text/html," + $('.edd_sl_license_key').text()
})[0].click()
$temp.remove();
});
我尝试使用JQuery html()
函数而不是text()
,尝试设置data:text/plain;charset=utf-8
。不走运。:(
JSFiddle这里:https://jsfiddle.net/movs21/vy4ukdpr/
发布于 2021-12-27 23:48:23
需要使用encodeURIComponent( text )
,也可以将\r\n替换为%0D%0A:replace('\n', '%0A', text)
。
同样的问题:Javascript export text file not recognizing \r\n in firefox
https://stackoverflow.com/questions/70496977
复制相似问题