Javascript text.txt文件中的区域内容,但保留换行符
我有一段代码将textarea的值保存到本地文本文件中。一切似乎都很好,但我不想失去断线。代码和小提琴: HTMLJavascript text.txt文件中的区域内容,但保留换行符,javascript,html,textarea,line-breaks,Javascript,Html,Textarea,Line Breaks,我有一段代码将textarea的值保存到本地文本文件中。一切似乎都很好,但我不想失去断线。代码和小提琴: HTML 有什么方法可以保持这些换行符吗?请帮帮我!谢谢。您可以使用replace 像这样: text = text.replace(/\n/g, "\r\n"); var data = new Blob([text], {type: 'text/plain'}); (function () { var textFile = null, makeTextFile
有什么方法可以保持这些换行符吗?请帮帮我!谢谢。您可以使用
replace
像这样:
text = text.replace(/\n/g, "\r\n");
var data = new Blob([text], {type: 'text/plain'});
(function () {
var textFile = null,
makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
var create = document.getElementById('create'),
textbox = document.getElementById('textbox');
create.addEventListener('click', function () {
var link = document.getElementById('downloadlink');
link.href = makeTextFile(textbox.value);
link.style.display = 'block';
}, false);
})();
text = text.replace(/\n/g, "\r\n");
var data = new Blob([text], {type: 'text/plain'});