在 HTML 文本区域中添加换行符

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

Add a linebreak in an HTML text area

html

提问by Nimesh Madhavan

How can i add a line break to the text area in a html page? i use VB.net for server side coding.

如何在 html 页面的文本区域添加换行符?我使用 VB.net 进行服务器端编码。

回答by jonas

If it's not vb you can use 
(ascii codes for cr,lf)

如果不是 vb,则可以使用
(cr,lf 的 ascii 代码)

回答by Loren Segal

Add a linefeed ("\n") to the output:

在输出中添加换行符(“\n”):

<textarea>Hello


Bybye</textarea>

Will have a newline in it.

里面会有一个换行符。

回答by Forgotten Semicolon

You could use \r\n, or System.Environment.NewLine.

您可以使用\r\n, 或System.Environment.NewLine

回答by user11334

If you're inserting text from a database or such (which one usually do), convert all "<br />"'s to &vbCrLf. Works great for me :)

如果您从数据库或类似数据库中插入文本(通常这样做),请将所有“ <br />”转换为 &vbCrLf。对我很有用:)

回答by SpoonMeiser

In a text area, as in the form input, then just a normal line break will work:

在文本区域中,就像在表单输入中一样,只需要一个普通的换行符即可:

<textarea>
This is a text area
line breaks are automatic
</textarea>

If you're talking about normal text on the page, the <br /> (or just <br> if using plain 'ole HTML4) is a line break.

如果您在谈论页面上的普通文本,则 <br /> (或者如果使用纯 'ole HTML4,则只是 <br> )是一个换行符。

However, I'd say that you often don't actually want a line break. Usually, your text is seperated into paragraphs:

但是,我想说的是,您通常实际上并不想要换行符。通常,您的文本被分成多个段落:

<p>
  This is some text
</p>
<p>
  This is some more
</p>

Which is much better because it gives a clue as to how your text is structured to machines that read it. Machines that read it include screen readers for the partially sighted or blind, seperating text into paragraphs gives it a chance of being presented correctly to these users.

哪个更好,因为它提供了有关您的文本如何被机器阅读的线索。阅读它的机器包括适用于弱视或盲人的屏幕阅读器,将文本分成段落使其有机会正确呈现给这些用户。

回答by Emu Malik

Escape sequences like "\n" work fine ! even with text area! I passed a java string with the "\n" to a html textarea and it worked fine as it works on consoles for java!

像 "\n" 这样的转义序列工作正常!即使有文本区域!我将带有 "\n" 的 java 字符串传递给了 html textarea,它在 java 控制台上运行良好!

回答by Dice

Here is my method made with pure PHP and CSS :

这是我用纯 PHP 和 CSS 制作的方法:

/** PHP code    */
<?php
    $string = "the string with linebreaks";
    $string = strtr($string,array("."=>".\r\r",":"=>" : \r","-"=>"\r - "));
?>

And the CSS :

和 CSS :

.your_textarea_class {
style='white-space:pre-wrap';
}

You can do the same with regex (I'm learning how to build regex with pregreplace using an associative array, seems to be better for adding the \n\r which makes the breaks display).

您可以对正则表达式执行相同操作(我正在学习如何使用关联数组使用 pregreplace 构建正则表达式,似乎更适合添加使中断显示的 \n\r)。

回答by Garry Shutler

I believe this will work:

我相信这会奏效:

TextArea.Text = "Line 1" & vbCrLf & "Line 2"

System.Environment.NewLine could be used in place of vbCrLf if you wanted to be a little less VB6 about it.

System.Environment.NewLine 可以用来代替 vbCrLf,如果你想少一点 VB6 的话。