我想在文本区添加换行符。我尝试了\n和<br/>标签,但不工作。你可以看到上面的HTML代码。你能帮我在文本区插入换行符吗?

<textarea cols='60' rows='8'>This is my statement one.\n This is my statement2</textarea>

<textarea cols='60' rows='8'>This is my statement one.<br/> This is my statement2</textarea>

当前回答

你可能想用\n而不是/n。

其他回答

要在文本区域内获得一个新行,在那里放一个实际的换行符:

<textarea cols='60' rows='8'> 这是我的statement2</textarea>

经过大量的测试,下面的代码在typescript中为我工作

 export function ReplaceNewline(input: string) {
    var newline = String.fromCharCode(13, 10);
    return ReplaceAll(input, "<br>", newline.toString());
}
export function ReplaceAll(str, find, replace) {
    return str.replace(new RegExp(find, 'g'), replace);
}

你可能想用\n而不是/n。

我的.replace()函数使用其他答案中描述的模式无法工作。在我的案例中起作用的模式是:

var str = "Test\n\n\Test\n\Test";
str.replace(/\r\n|\r|\n/g,'&#13;&#10;');

// str: "Test&#13;&#10;&#13;&#10;Test&#13;&#10;Test"

T.innerText = "Position of LF: " + t.value.indexOf("\n");

p3.innerText = t.value.replace("\n", "");

<textarea id="t">Line 1&#10;Line 2</textarea>

<p id='p3'></p>