HTML 在 textarea 中显示换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13484985/
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 03:59:05  来源:igfitidea点击:

HTML Display line breaks within textarea

htmltextarealine-breaks

提问by user1260310

I have asked this question now several ways, but still have not gotten an answer.

我现在已经通过多种方式问了这个问题,但仍然没有得到答案。

When you capture asci text in a textarea that includes line breaks, it seems to insert invisible characters in the form of line breaks when you hit return. The line break seems to have the format \r for return or \n for new line. These characters are not visible in the text but are there somewhere.

当您在包含换行符的 textarea 中捕获 asci 文本时,它似乎在您按回车时以换行符的形式插入不可见的字符。换行符的格式似乎是 \r 表示返回或 \n 表示换行。这些字符在文本中不可见,但在某处。

However, when I put this code in a textarea, I cannot get it to display a linebreak. In fact, I cannot find any code that placed between textarea tags displays a line break.

但是,当我将此代码放在 textarea 中时,我无法让它显示换行符。事实上,我找不到放置在 textarea 标签之间的任何代码显示换行符。

Can someone show a way to display line breaks in a textarea, ie.

有人可以展示一种在文本区域中显示换行符的方法,即。

<textarea>first line <some code> second line </textarea>

I have really tried about everything you could possibly imagine. For example, if I try <textarea> first line "\r\n" second line</textarea>it just displays

我真的尝试过你能想象到的一切。例如,如果我尝试<textarea> first line "\r\n" second line</textarea>它只会显示

`firstline "\r\n"` second line

Note: anything involving <br>will not solve this problem as this is not about html, but rather plain text displaying in textarea box.

注意:任何涉及<br>都不能解决这个问题,因为这不是关于 html,而是在 textarea 框中显示的纯文本。

Thanks for suggested code to display line breaks in textarea.

感谢您提供在 textarea 中显示换行符的建议代码。

回答by Jeremy Morehouse

Well if you're doing this through pure HTML, then you can do it one of two ways. Just add lines by pressing enter or using the ASCII characters &#013; &#010;

好吧,如果您通过纯 HTML 执行此操作,那么您可以通过以下两种方式之一执行此操作。只需按 Enter 键或使用 ASCII 字符添加行&#013; &#010;

jsFiddle

js小提琴

回答by stomtech

If you are using javascriptyou can use the textarea element value Property.

如果您使用的是javascript,则可以使用 textarea 元素值 Property

var tb = document.getElementById("tb");

var newLine = "\r\n";

var text = "Output: " + newLine + "-------------------" + 
                        newLine + "line 1: " + 
                        newLine + "line 2: ";

tb.value = text;
//console.log(text);
<textarea id="tb" rows=5>
Output: &#13;&#10;-------------
</textarea>

Run it on JSFiddle

在 JSFiddle 上运行它

回答by Code-Source

I am not sure to anderstand your question.

我不确定是否能理解您的问题。

If you want to write this in your <textarea/>

如果你想在你的 <textarea/>

first line
second line

You should write this:

你应该这样写:

<html>
    <head>
        <title></title>
    </head>
    <body>
        <textarea rows="4" cols="40">
first line
second line
        </textarea>
    </body>
</html>

In PHP you should write this

在 PHP 中你应该写这个

<html>
    <head>
        <title></title>
    </head>
    <body>
        <textarea rows="4" cols="40"><?php echo "first line\r\nsecond line" ?></textarea>
    </body>
</html>

Make sure that string is between " and not '

确保字符串介于 " 和 ' 之间