简体   繁体   English

显示textarea的多行纯文本?

[英]Show multiline plain text of textarea?

I just want to show multiline plain text of textarea but I don't want to show the textarea. 我只想显示textarea的多行纯文本,但我不想显示textarea。

Example: 例:

Textarea: 多行文本:

|===================|
| First line        |
| Second line       |
|___________________|

Multiline plain text: 多行纯文本:

First line
Second line

How can I do this? 我怎样才能做到这一点?

You can use <pre> , which defines preformatted text and preserves both spaces and line breaks: 您可以使用<pre> ,它定义预格式化的文本并保留空格和换行符:

var txt="First line\nSecond line";
document.body.innerHTML+="<pre>"+txt+"</pre>";

Or if you want to do it with CSS or you want more options about whitespaces and linebreaks, you can use CSS property white-space : 或者,如果您想使用CSS来做,或者想要更多有关空格和换行符的选项,则可以使用CSS属性white-space

Javascript: 使用Javascript:

var txt="First line\nSecond line";
document.getElementById('mydiv').innerHTML=txt;

CSS: CSS:

#mydiv{white-space:pre;}

CSS for textarea: 用于textarea的CSS:

textarea { 
    border-style: none; 
    border-color: Transparent; 
    overflow: auto;        
  }

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

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