简体   繁体   English

HTML中的空白文本区域

[英]White space textarea in html

I have a website that has a form. 我有一个具有表单的网站。 In the form I have a textarea field. 在表单中,我有一个textarea字段。 When this is filled and displayed in the manner below 当以以下方式填充并显示时

eg . 例如。 this is how it was written in the textarea box 这是在textarea框中写的

The manager,

Abc limited,

private bag,

earth.

It displayed like this 它像这样显示

The manager, Abc limited, private bag, earth.

how can I make it stay the way it was written 我怎样才能使其保持原样

You can use the innerText attribute of the element that will hold the textarea value. 您可以使用将保留textarea值的元素的innerText属性。

Important: At the backend side, you have to preserve the \\n in the Database or whatever it's being used to store the data to get it back and render the content exactly as was saved. 重要提示:在后端,您必须将\\n保留在数据库中或用于存储数据的任何内容,以将其取回并完全按保存的方式呈现内容。

 var div = document.querySelector('div'); var textarea = document.querySelector('textarea'); div.innerText = textarea.value; textarea.addEventListener('input', function() { div.innerText = this.value; }); 
 <h3>Enter text and press enter</h3> <small>The entered text will appear automatically</small> <p> <textarea> The manager, Abc limited, private bag, earth.</textarea> <div></div> 

The only way I was able to recreate your error was by misspelling <textarea> as <text area> . 我能够重新创建错误的唯一方法是将<textarea>拼写为<text area> When the space is added, the error occurs. 添加空格时,会发生错误。 When properly spelled line breaks are preserved. 保留正确拼写的换行符后。

您必须使用\\ntextarea内换行

If you are using php, then you can echo echo nl2br($textarea); 如果您使用的是php,则可以echo echo nl2br($ textarea);

https://www.w3schools.com/php/func_string_nl2br.asp https://www.w3schools.com/php/func_string_nl2br.asp

or for jquery 或对于jQuery

jQuery convert line breaks to br (nl2br equivalent) jQuery将换行符转换为br(相当于nl2br)

Well you are not really giving us a lot to go on how you print the text field. 好吧,您实际上并没有给我们提供很多方法来打印文本字段。 But my guess is that if you use php you should wrap your variable in a nl2br() function. 但是我的猜测是,如果您使用php,则应该将变量包装在nl2br()函数中。

Get more information here: http://php.net/manual/en/function.nl2br.php 在此处获取更多信息: http : //php.net/manual/en/function.nl2br.php

HTML会忽略新行/空格,除非您使用本示例中的 white-space:pre样式设置元素

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

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