How can I show HTML formatting inside a textarea? [duplicate]

偶尔善良 提交于 2019-12-10 13:47:56

问题


I have code that lets you format the text in a textarea with bold, underline, strikeout or italics. But it displays the html tags instead of actually formatting it. I know once I output it inside a div, it ill be formatted, but I want to show the formatting while the user writes the text.

How can I enable formatting? JavaScript is fine, I'm using JQuery at the moment. I would also rather not use a WYSIWYG editor, if there is another option.

Thanks for any help.


回答1:


Use contentEditable attribute on div element

 <div contentEditable="true"> 
 </div

This way you can format your input in any desired way, just dont forget properly escape data.




回答2:


Just a thought; if you just want to let users see what their response will look like, you could take a cue from this site and have a sort of "preview" div right above/below the comment box. You could use onKeyUp to put the code into the div to keep it up do date as the user types. It's not perfect, but it's probably as good as you're going to get if you don't want to use a WYSIWYG.

That being said, the contentEditable + WYSIWYG option is probably more user friendly, and simpler, IMHO.




回答3:


I believe it's only possible by using some div or other styleable html element instead of your textarea. With Javascript you would then add the functionality of the textarea.

How about you use some formatting system like this here on SO? It is much more elegant in my opinion, than pseudo WYSIWYG.




回答4:


Replace the textarea with an div and enable the designmode. Or you can use the jquery plugin http://plugins.jquery.com/project/designMode




回答5:


Cant be done inside of an Textarea, you will have to go with making a wysiwyg editor or something like that. Its pretty easy to make one of those yourself. This is a pretty good tutorial:

Devgurus wysiwyg tutorial



来源:https://stackoverflow.com/questions/7785607/how-can-i-show-html-formatting-inside-a-textarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!