javascript 你能在 <textarea> 中有一个 html 列表,它看起来像一个列表吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4095690/
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
Can you have an html list inside a <textarea> and it look like a list?
提问by chromedude
I tried
我试过
<textarea rows="10" columns="2"><li>joe</li><li>jerry</li></textarea>
to see if I could put an html list in a <textarea>but it just showed as text. Is there any way to show it as a list inside the textarea?
看看我是否可以将 html 列表放入 a 中,<textarea>但它只显示为文本。有没有办法将它显示为文本区域内的列表?
回答by Yi Jiang
You can't have HTML inside textareas - it's for plain text only. However, you cantry using contentEditablefor this. The result isn't very good, but you be the judge whether you want to use it or not. Simply use something like this:
您不能在textareas 中包含 HTML - 它仅用于纯文本。但是,您可以尝试使用contentEditable它。结果不是很好,但你要判断你是否要使用它。只需使用这样的东西:
<div class="editable" contenteditable="true">
<ul>
<li>List item</li>
<li>List item</li>
</ul>
</div>
The problems with this method is that it's difficult to predict what HTML the browser will produce, and that some of the HTML produced are truly atrocious, and that you'll have to sanitise the input a lot more than using plain text. Also, the interface for this isn't specified in the specifications, so you'll be dealing with different interfaces in different browsers.
这种方法的问题在于,很难预测浏览器将生成什么 HTML,并且生成的某些 HTML 确实很糟糕,而且与使用纯文本相比,您必须对输入进行更多的清理。此外,规范中未指定用于此的接口,因此您将在不同的浏览器中处理不同的接口。
See: http://www.jsfiddle.net/yijiang/bN2tm/and http://html5demos.com/contenteditable
参见:http: //www.jsfiddle.net/yijiang/bN2tm/和http://html5demos.com/contenteditable
回答by Oded
This is not possible.
这不可能。
textareais for displaying and editing text, not HTML.
textarea用于显示和编辑文本,而不是 HTML。
回答by Pedro Jacinto
It's not possible to have your HTML inside a <textarea>rendered as HTML.
不可能将您的 HTML<textarea>呈现为 HTML。
If you want your users to be able to edit and modify the list contents you can try using a WYSIWYG HTML editor like FCKEditoror TinyMCE.
如果您希望您的用户能够编辑和修改列表内容,您可以尝试使用 WYSIWYG HTML 编辑器,如FCKEditor或TinyMCE。
It's a bit of an overkill if you just want the list functionality and none of the other rich text editor capabilities, but both of those editors can be fully customized an trimmed down. Check the FCKEditor demonamed "Custom Toobar" for an example.
如果您只需要列表功能而不需要任何其他富文本编辑器功能,这有点矫枉过正,但是这两个编辑器都可以完全自定义和修剪。以名为“Custom Toobar”的FCKEditor 演示为例。

