简体   繁体   English

在文本区域内渲染 HTML

[英]Rendering HTML inside textarea

I need to be able to render some HTML tags inside a textarea (namely <strong>, <i>, <u>, <a>) but textareas only interpret their content as text.我需要能够在 textarea(即 <strong>、<i>、<u>、<a>)内呈现一些 HTML 标签,但 textareas 仅将其内容解释为文本。 Is there an easy way of doing it without relying on external libraries/plugins (I'm using jQuery)?有没有不依赖外部库/插件的简单方法(我正在使用 jQuery)? If not, do you know of any jQuery plugin I could use to do this?如果没有,您是否知道我可以使用任何 jQuery 插件来执行此操作?

This is not possible to do with a textarea .这对于textarea是不可能的。 What you are looking for is an content editable div, which is very easily done:您正在寻找的是一个内容可编辑的div,这很容易完成:

<div contenteditable="true"></div>

jsFiddle js小提琴

 div.editable { width: 300px; height: 200px; border: 1px solid #ccc; padding: 5px; } strong { font-weight: bold; }
 <div contenteditable="true">This is the first line.<br> See, how the text fits here, also if<br>there is a <strong>linebreak</strong> at the end? <br>It works nicely. <br> <br><span style="color: lightgreen">Great</span>. </div>

With an editable div you can use the method document.execCommand ( more details ) to easily provide the support for the tags you specified and for some other functionality..使用可编辑的 div,您可以使用document.execCommand方法( 更多详细信息)轻松为您指定的标签和其他一些功能提供支持。

 #text { width : 500px; min-height : 100px; border : 2px solid; }
 <div id="text" contenteditable="true"></div> <button onclick="document.execCommand('bold');">toggle bold</button> <button onclick="document.execCommand('italic');">toggle italic</button> <button onclick="document.execCommand('underline');">toggle underline</button>

Since you only said render, yes you can.既然你只说渲染,是的,你可以。 You could do something along the lines of this:你可以做一些类似的事情:

 function render(){ var inp = document.getElementById("box"); var data = ` <svg xmlns="http://www.w3.org/2000/svg" width="${inp.offsetWidth}" height="${inp.offsetHeight}"> <foreignObject width="100%" height="100%"> <div xmlns="http://www.w3.org/1999/xhtml" style="font-family:monospace;font-style: normal; font-variant: normal; font-size:13.3px;padding:2px;;"> ${inp.value} <i style="color:red">cant touch this</i> </div> </foreignObject> </svg>`; var blob = new Blob( [data], {type:'image/svg+xml'} ); var url=URL.createObjectURL(blob); inp.style.backgroundImage="url("+URL.createObjectURL(blob)+")"; } onload=function(){ render(); ro = new ResizeObserver(render); ro.observe(document.getElementById("box")); }
 #box{ color:transparent; caret-color: black; font-style: normal;/*must be same as in the svg for caret to align*/ font-variant: normal; font-size:13.3px; padding:2px; font-family:monospace; }
 <textarea id="box" oninput="render()">you can edit me!</textarea>
This makes it so that a textarea will render html! 这使得textarea将呈现 html! Besides the flashing when resizing, inability to directly use classes and having to make sure that the div in the svg has the same format as the textarea for the caret to align correctly, it's works! 除了在调整大小时闪烁,无法直接使用类以及必须确保svg中的 div 与textarea具有相同的格式以便插入符号正确对齐,它的工作原理!

try this example试试这个例子

 function toggleRed() { var text = $('.editable').text(); $('.editable').html('<p style="color:red">' + text + '</p>'); } function toggleItalic() { var text = $('.editable').text(); $('.editable').html("<i>" + text + "</i>"); } $('.bold').click(function() { toggleRed(); }); $('.italic').click(function() { toggleItalic(); });
 .editable { width: 300px; height: 200px; border: 1px solid #ccc; padding: 5px; resize: both; overflow: auto; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="editable" contenteditable="true"></div> <button class="bold">toggle red</button> <button class="italic">toggle italic</button>

An addendum to this.对此的补充。 You can use character entities (such as changing <div> to &lt;div&gt; ) and it will render in the textarea.可以使用字符实体(如改变<div>&lt;div&gt;它会在textarea的渲染。 But when it is saved, the value of the textarea is the text as rendered .但是当它被保存时,textarea 的值是呈现的文本。 So you don't need to de-encode.所以你不需要去编码。 I just tested this across browsers (ie back to 11).我刚刚跨浏览器测试了这个(即回到 11)。

I have the same problem but in reverse, and the following solution.我有同样的问题,但反过来,还有以下解决方案。 I want to put html from a div in a textarea (so I can edit some reactions on my website; I want to have the textarea in the same location.)我想将 div 中的 html 放在 textarea 中(这样我就可以在我的网站上编辑一些反应;我希望 textarea 位于同一位置。)

To put the content of this div in a textarea I use:要将这个 div 的内容放在我使用的 textarea 中:

 var content = $('#msg500').text(); $('#msg500').wrapInner('<textarea>' + content + '</textarea>');
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="msg500">here some <strong>html</strong> <i>tags</i>.</div>

This is possible with <textarea> the only thing you need to do is use Summernote WYSIWYG editor这可以通过<textarea> ,您唯一需要做的就是使用Summernote WYSIWYG 编辑器

it interprets HTML tags inside a textarea (namely <strong> , <i> , <u> , <a> )它解释文本区域内的 HTML 标签(即<strong><i><u><a>

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

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