Html 在 <textarea> 中格式化文本?

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

Format text in a <textarea>?

csshtmltextarea

提问by Don P

Textareas are great because of some built in functionality (scrollbars). How can I format <spans>of text inside of the <textarea>?

由于一些内置功能(滚动条),Textareas 很棒。我怎样才能格式化<spans>里面的文本<textarea>

回答by Alcides Queiroz Aguiar

If you need to customize your textarea, reproduce its behavior using another element (like a DIV) with the contenteditableattribute.

如果您需要自定义您的 textarea,请使用具有该contenteditable属性的另一个元素(如 DIV)重现其行为。

It's more customizable, and a more modern approach, textarea is just for plain text content, not for rich content.

它更具可定制性,也是一种更现代的方法,textarea 仅适用于纯文本内容,不适用于丰富的内容。

<div id='fake_textarea' contenteditable></div>

The scrollbars can be reproduced with the CSS overflow property.

滚动条可以使用 CSS 溢出属性重现。

You can use this fake textarea in a form normally, i.e: if you have to submit its content through POST method, you could do something like(with jQuery):

您可以在表单中正常使用这个伪造的 textarea,即:如果您必须通过 POST 方法提交其内容,您可以执行以下操作(使用 jQuery):

<input type='hidden' id='fake_textarea_content' name='foobar' />

...

...

$('#your_form').submit(function()
{
  $('#fake_textarea_content').val($('#fake_textarea').html());
});

回答by Deep

You Can't style the content of a text area separately, you have to use <div>s, or something similar.

您不能单独设置文本区域的内容样式,您必须使用<div>s 或类似的东西。

Do you Want Something like this:?

你想要这样的东西:?

http://jsfiddle.net/mekwall/XNkDx/

http://jsfiddle.net/mekwall/XNkDx/

$('.editable').each(function(){
    this.contentEditable = true;
});

This allows you to edit the content of a div, and it will still look like a textarea,

这允许你编辑一个 div 的内容,它仍然看起来像一个 textarea,

Bold will now Work.

Bold 现在可以工作了。

回答by Marat Tanalin

You cannot use HTML inside TEXTAREA.

你不能在里面使用 HTML TEXTAREA

Scrolling can be applied to any element by adding overflow: autoand fixed widthand/or height.

通过添加overflow: auto和固定width和/或 ,可以将滚动应用于任何元素height

回答by kaustubh

You can user html editors for web like CKEditor to be able to format the data in text area. Check this http://ckeditor.com/

您可以使用像 CKEditor 这样的 web html 编辑器来格式化文本区域中的数据。检查这个http://ckeditor.com/

回答by Christian

Another way to submit the "fake" text area is including the following lines inside the form tag

提交“假”文本区域的另一种方法是在表单标签中包含以下几行

<form onsubmit=" $('#fake_textarea_content').val($('#fake_textarea').html());">
</form>