Html 在 textarea 中强制回车

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5082498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:53:46  来源:igfitidea点击:

Forcing a carriage return in textarea

htmltextarea

提问by Masiar

I would like to start a textarea with inside a text that starts some line under the first line. Doing something like:

我想在文本中开始一个文本区域,该文本在第一行下的某行开始。做类似的事情:

var myText = '\r \r \r HELLO';

doesn't work: HELLO is written on the first line, while

不起作用:HELLO 写在第一行,而

var myText = 'HELLO \r \r \r HELLO2';

puts correctly HELLO2 after HELLO. This means that \ris correct, but it doesn't work at the beginning of the textarea.

在 HELLO 之后正确放置 HELLO2。这意味着这\r是正确的,但它在 textarea 的开头不起作用。

Any suggestions?

有什么建议?

采纳答案by parrker9

Have you tried to put space or  before the "\r"?

你有没有试过 在“\r”之前放空格?

回答by Rick Presley

Try inserting 
into the text area.

尝试插入
文本区域。

回答by selbie

Don't use \r, use \n

不要用\r,用\n

Example: var myText = '\n \n \n HELLO';

示例:var myText = '\n \n \n HELLO';

回答by Aaron

In PHP I had to make sure to use double quotes instead of single.

在 PHP 中,我必须确保使用双引号而不是单引号。

$var1 = 'Test1 above /n/n Test below'; //will not work
$var2 = "Test2 above /n/n Test below"; //will work
echo = "<textarea>$var1</textarea><br /><textarea>$var2</textarea>";

回答by Carlos Valenzuela

"\r" is to return to the begining of the line the, i forgot its name, i think is the cursor. But what you need is "\n" for a new line

“\r”是返回到行首的,我忘了它的名字,我认为是光标。但是你需要的是 "\n" 换行

回答by JonWillis

If you are using .net.

如果您使用的是 .net。

Then do

然后做

Var myText = "Hello" + Enviroment.NewLine + "Hello2";

Var myText = "Hello" + Enviroment.NewLine + "Hello2";

However if your using something else, i.e. Java, web languages then I advise you try /nfollowed by /r. I have had to use "/n/r"to get the result I wanted, after the /rwas being ignored.

但是,如果您使用其他语言,即 Java、Web 语言,那么我建议您尝试/n后跟/r. 在被忽略"/n/r"之后,我不得不使用来获得我想要的结果/r

回答by josh3736

What browser and/or OS are you using? In my quick test, both \rand \nyield the same (correct) result on Windows in IE 8, Firefox 3.6, and Chrome 9.

您使用的是什么浏览器和/或操作系统? 在我的快速测试,都\r\n在IE 8,火狐3.6和Chrome 9产生在Windows相同的(正确的)结果。

 

 

<textarea rows="5" id="r"></textarea>
<textarea rows="5" id="n"></textarea>

 

 

$(function() {
    $('#r').val('\r\r\rHello');
    $('#n').val('\n\n\nHello');
});