[英]Cannot set multiline text in textarea
I have an array, each of its elements in a text line:我有一个数组,它的每个元素都在一个文本行中:
a = ['line1', 'line2', 'line3'];
I want to set the text lines into a textarea, displayed like so:我想将文本行设置为一个 textarea,显示如下:
line1
line2
line3
I set the text with the following code:我使用以下代码设置文本:
myTxtArea.value = a.join('\n');
and it shows like this:它显示如下:
line1\nline2\nline3
What to do so that the \\n
's break lines instead of being displayed?该怎么做才能使\\n
的断行线不被显示?
The textarea HTML:文本区域 HTML:
<textarea autofocus id=laidTxt name=laidTxt class="laidTxt"
placeholder="paste PDF text here">KJHKH DSKJH ...
</textarea>
I see我懂了
try this code试试这个代码
for (let index = 0; index < a.length; index++) {
myTxtArea.value += a[index]+'\n';
}
its work with me它和我一起工作
I dont know why it does not work for you but as it shows in the example below it works我不知道为什么它对你不起作用,但正如它在下面的例子中显示的那样有效
a = ['line1', 'line2', 'line3']; document.getElementById("laidTxt").value = a.join('\\n');
<textarea autofocus id="laidTxt"></textarea>
To clear any doubts, the code I initially posted in the question is OK and works fine.为了消除任何疑问,我最初在问题中发布的代码是可以的并且工作正常。
For the curious: I was applying a JSON.stringify()
transformation to the joined lines just before setting them into the textarea.出于好奇:在将它们设置到 textarea 之前,我正在对连接的行应用JSON.stringify()
转换。
Like so:像这样:
let txt = JSON.stringify( handleText( txtArea.value ) );
txtArea.value = txt;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.