New line in text area

2018-12-31 17:24发布

<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>

I tried both but new line is not reflecting while rendering the html file. How can I do that?

标签: html
12条回答
冷夜・残月
2楼-- · 2018-12-31 18:12

just use <br>
ex:

<textarea>
blablablabla <br> kakakakakak <br> fafafafafaf 
</textarea>

result:
blablablabla
kakakakakak
fafafafafaf

查看更多
怪性笑人.
3楼-- · 2018-12-31 18:21

Try this one:

    <textarea cols='60' rows='8'>This is my statement one.&#13;&#10;This is my statement2</textarea>

&#10; Line Feed and &#13; Carriage Return are HTML entitieswikipedia. This way you are actually parsing the new line ("\n") rather than displaying it as text.

查看更多
若你有天会懂
4楼-- · 2018-12-31 18:22

To get a new line inside text-area, put an actual line-break there:

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

查看更多
临风纵饮
5楼-- · 2018-12-31 18:23

try this.. it works:

<textarea id="test" cols='60' rows='8'>This is my statement one.&#10;This is my statement2</textarea>

replacing for
tags:

$("textarea#test").val(replace($("textarea#test").val(), "<br>", "&#10;")));
查看更多
墨雨无痕
6楼-- · 2018-12-31 18:28

If you are talking about using the value of the textarea somewhere else on your page and rendering the newlines, I found the css propperty white-space: pre-wrap; to work very well, and it's a simple solution. Maybe this helps someone.

查看更多
裙下三千臣
7楼-- · 2018-12-31 18:30

My .replace()function using the patterns described on the other answers did not work. The pattern that worked for my case was:

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"
查看更多
登录 后发表回答