javascript | save textarea value with line breaks

前端 未结 2 553
失恋的感觉
失恋的感觉 2020-12-14 18:59

I have a code where it saves multiple textarea values in a text file. However, it does not display the line breaks I indicated after saving it. It only identifies the line b

2条回答
  •  萌比男神i
    2020-12-14 19:21

    The problem stems from the fact that line breaks (\n) are not the same as HTML
    tags.

    Try this:

    var text = document.forms[0].txt.value;
    text = text.replace(/\n\r?/g, '
    ');

    Edit, try this as the js:

    var text = document.forms[0].txt.value;
    
    if (text === true) { text = text.replace(/\n\r?/g, '
    '); } var TestVar = new Array(i); var i = 0; function save() { TestVar[i] = document.getElementById("text1").value + "/n" + document.getElementById("text2").value; mydoc = document.open(); mydoc.write(TestVar); mydoc.execCommand("saveAs",true,"TicketID.txt"); mydoc.close(); }

提交回复
热议问题