简体   繁体   English

FireFox 中的 JQuery text() 中丢失了新行

[英]Lost new-lines in JQuery text() in FireFox

I have a DIV at the page which I want user to be able to download as a text file (this is a license key).我在页面上有一个 DIV,我希望用户能够将其作为文本文件下载(这是一个许可证密钥)。 This DIV has \n symbols which is correctly displayed using white-space: pre;此 DIV 具有使用white-space: pre; \n符号:pre; . . I use the code bellow to create temporary <a> link with DIV content and download it.我使用下面的代码创建带有 DIV 内容的临时<a>链接并下载它。 This works fine in Chrome, but FireFox loose the new-line symbols and I get single-lined text.这在 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();
});

I tried to use JQuery html() function instead of text() , tried to set up data:text/plain;charset=utf-8 .我尝试使用 JQuery html() function 而不是text() ,尝试设置data:text/plain;charset=utf-8 No luck.没运气。 :( :(

JSFiddle here: https://jsfiddle.net/movs21/vy4ukdpr/ JSFiddle: https://jsfiddle.net/movs21/vy4ukdpr/

Need to use encodeURIComponent( text ) , also it's possible to replace \r\n with %0D%0A: replace('\n', '%0A', text) .需要使用encodeURIComponent( text ) ,也可以将 \r\n 替换为 %0D%0A: replace('\n', '%0A', text)

Same question here: Javascript export text file not recognizing \r\n in firefox同样的问题: Javascript export text file not recognize \r\n in firefox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM