如何向 HTML 文本区域添加换行符

发布于 2024-07-20 07:42:12 字数 144 浏览 10 评论 0原文

我正在使用 JavaScript 编辑

我正在获取编写函数的值,但它不会给出换行符。

I’m editing a <textarea> with JavaScript. The problem is that when I make line breaks in it, they won’t display. How can I do this?

I’m getting the value to write a function, but it won’t give line breaks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

能怎样 2024-07-27 07:42:13

我有一个 id 为 #infoartist 的文本区域,如下所示:

<textarea id="infoartist" ng-show="dForm" style="width: 100%;" placeholder="Tell your contacts and collectors about yourself."></textarea>

在 JavaScript 代码中,我将获取文本区域的值,并用 替换转义新行 (\n\r)
标签,如:

var text = document.getElementById("infoartist").value;
text = text.replace(/\r?\n/g, '<br />');

所以如果你使用 jQuery(像我一样):

var text = $("#infoartist").val();
text = text.replace(/\r?\n/g, '<br />');

I have a textarea whose id is #infoartist, as follows:

<textarea id="infoartist" ng-show="dForm" style="width: 100%;" placeholder="Tell your contacts and collectors about yourself."></textarea>

In JavaScript code, I’ll get the value of the textarea and replace escaping a new line (\n\r) by the <br /> tag, such as:

var text = document.getElementById("infoartist").value;
text = text.replace(/\r?\n/g, '<br />');

So if you are using jQuery (like me):

var text = $("#infoartist").val();
text = text.replace(/\r?\n/g, '<br />');
看透却不说透 2024-07-27 07:42:13

对于浏览器来说,新行只是空白,不会被视为与普通空格(“”)有任何不同。 要获取新行,您必须插入
元素。

解决该问题的另一种尝试:在文本区域中输入文本,然后在按钮后面添加一些 JavaScript,将不可见字符转换为可读字符,并将结果转储到 DIV 中。 这会告诉你你的浏览器想要什么。

A new line is just whitespace to the browser and won't be treated any different to a normal space (" "). To get a new line, you must insert <BR /> elements.

Another attempt to solve the problem: Type the text into the textarea and then add some JavaScript behind a button to convert the invisible characters to something readable and dump the result to a DIV. That will tell you what your browser wants.

彩扇题诗 2024-07-27 07:42:13

如果您只需要将文本区域的值发送到服务器并换行,请使用 nl2br

If you just need to send the value of the textarea to the server with line breaks, use nl2br.

绿萝 2024-07-27 07:42:13

这是我为遇到同样的麻烦所做的事情。

当我将文本传递到 JSP 中的下一页时,我将其读为文本区域而不是读取类似的内容

<s:property value="%{text}"/>

,因此输出如您所愿。

对于其他属性,您可以按如下方式使用它。

<textarea style="background-color: white; border: none; width:660px;font-family: Arial, Helvetica, sans-serif; font-size:1.0em; resize:none;" name="text" cols="75" rows="15" readonly="readonly" ><s:property value="%{text}"/></textarea>

Here is the thing I did for the same trouble I had.

When I'm passing the text to the next page in JSP, I’m reading it as a textarea instead of reading something like

<s:property value="%{text}"/>

so the output came as you wanted.

And for other properties, you can use it as below.

<textarea style="background-color: white; border: none; width:660px;font-family: Arial, Helvetica, sans-serif; font-size:1.0em; resize:none;" name="text" cols="75" rows="15" readonly="readonly" ><s:property value="%{text}"/></textarea>
谁对谁错谁最难过 2024-07-27 07:42:12

问题来自于换行符 (\n\r?) 与 HTML
标记不同:

var text = document.forms[0].txt.value;
text = text.replace(/\r?\n/g, '<br />');

因为许多注释和我自己的经验告诉我,这个
解决方案没有按预期工作,下面是如何使用 '\ 将新行附加到 textarea 的示例r\n':

function log(text) {
    var txtArea;

    txtArea = document.getElementById("txtDebug");
    txtArea.value += text + '\r\n';
}

The problem comes from the fact that line breaks (\n\r?) are not the same as HTML <br/> tags:

var text = document.forms[0].txt.value;
text = text.replace(/\r?\n/g, '<br />');

Since many of the comments and my own experience have shown me that this <br> solution is not working as expected, here is an example of how to append a new line to a textarea using '\r\n':

function log(text) {
    var txtArea;

    txtArea = document.getElementById("txtDebug");
    txtArea.value += text + '\r\n';
}
拒绝两难 2024-07-27 07:42:12

如果您使用通用 JavaScript 并且需要将字符串分配给文本区域值,那么
document.getElementById("textareaid").value='texthere\\ntexttext'。

您需要替换 \n< br >\\\n

否则,它会在所有浏览器中给出Uncaught SyntaxError: Unexpected token ILLEGAL

If you use general JavaScript and you need to assign a string to a text area value, then
document.getElementById("textareaid").value='texthere\\ntexttext'.

You need to replace \n or < br > with \\\n.

Otherwise, it gives Uncaught SyntaxError: Unexpected token ILLEGAL in all browsers.

面犯桃花 2024-07-27 07:42:12

您需要在 textarea 内使用 \n 作为 linebreaks

You need to use \n for linebreaks inside textarea

〆凄凉。 2024-07-27 07:42:12

我遇到了从服务器变量传递到 JavaScript 变量的换行符问题,然后 JavaScript 将它们写入文本区域(使用 KnockoutJS 值绑定)。

解决方案是双重转义新行:

original.Replace("\r\n", "\\r\\n")

在服务器端,因为只有单个转义字符,JavaScript 无法解析。

I had a problem with line breaks which were passed from a server variable to a JavaScript variable, and then JavaScript was writing them to a textarea (using KnockoutJS value bindings).

The solution was double escaping new lines:

original.Replace("\r\n", "\\r\\n")

on the server side, because with just single escape characters, JavaScript was not parsing.

梦回旧景 2024-07-27 07:42:12

如果您想在自己的页面内显示文本,可以使用

 标记。

document.querySelector('textarea').addEventListener('keyup', function() {
  document.querySelector('pre').innerText = this.value;
});
<textarea placeholder="type text here"></textarea>
<pre style="font-family: inherits">
The
new lines will
be respected
      and spaces too
</pre>

If you want to display text inside your own page, you can use the <pre> tag.

document.querySelector('textarea').addEventListener('keyup', function() {
  document.querySelector('pre').innerText = this.value;
});
<textarea placeholder="type text here"></textarea>
<pre style="font-family: inherits">
The
new lines will
be respected
      and spaces too
</pre>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文