我想在文本区添加换行符。我尝试了\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。

其他回答

经过大量的测试,下面的代码在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);
}
<textarea cols='60' rows='8'>This is my statement one.

This is my statement2</textarea>

提琴显示它的工作:http://jsfiddle.net/trott/5vu28/。

如果你真的想让它在源文件中的单行上,你可以插入HTML字符引用,用于换行和回车,如@Bakudan的回答所示:

这是我的语句1。这是我的语句2</textarea>

一个简单自然的解决方案,不涉及CSS样式或数字字符引用,如&#13;&#10;将使用&换行符;字符实体引用:

The cardinal directions are:&NewLine;- North&NewLine;- East&NewLine;- South&NewLine;- West

注意:因为它被简单地定义为LF(换行,或U+000A Unicode码位)字符,所以不能100%确定它是否适合需要整个CR + LF(回车+换行)序列的情况。但后来,它在我在Windows 10上进行的Chrome、Edge和WebView2测试中都能运行,所以应该可以使用。

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

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

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