简体   繁体   English

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

[英]Add a linebreak in an HTML text area

How can i add a line break to the text area in a html page?如何在 html 页面的文本区域中添加换行符? i use VB.net for server side coding.我使用 VB.net 进行服务器端编码。

If it's not vb you can use 
如果不是 vb,你可以使用
 (ascii codes for cr,lf) (cr,lf的ASCII码)

Add a linefeed ("\n") to the output:在 output 中添加换行符(“\n”):

<textarea>Hello


Bybye</textarea>

Will have a newline in it.里面会有换行符。

You could use \r\n , or System.Environment.NewLine .您可以使用\r\nSystem.Environment.NewLine

If you're inserting text from a database or such (which one usually do), convert all " <br /> "'s to &vbCrLf.如果您从数据库或类似的数据库中插入文本(通常会这样做),请将所有“ <br /> ”转换为 &vbCrLf。 Works great for me:)对我很有用:)

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.阅读它的机器包括部分视力或盲人的屏幕阅读器,将文本分成段落使其有机会正确呈现给这些用户。

Escape sequences like "\n" work fine !像“\n”这样的转义序列可以正常工作! 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”的 java 字符串传递给了 html 文本区域,它运行良好,因为它适用于 Java 控制台!

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

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 关于它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM