在textarea中渲染HTML [英] Rendering HTML inside textarea

查看:608
本文介绍了在textarea中渲染HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在textarea(即< strong>,< i>,< u>< a>)内呈现一些HTML标签,但textareas只将它们的内容解释为文本。有没有一个简单的方法做到这一点,而不依赖于外部库/插件(我使用jQuery)?
如果没有,你知道我可以使用任何jQuery插件吗?

解决方案

这是不可能的如何处理 textarea 。您正在寻找的是 content editable div,这很容易完成:

 < div contenteditable =true>< / div> 

jsFiddle



  div.editable {width:300px ; height:200px; border:1px solid #ccc; padding:5px;} strong {font-weight:bold;}  

<$ c $ < div contenteditable =true>这是第一行。< br>请参阅这些文字如何适用,以及如果< strong>换行符< / strong> < br>< / div>< / code><


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. Is there an easy way of doing it without relying on external libraries/plugins (I'm using jQuery)? If not, do you know of any jQuery plugin I could use to do this?

解决方案

This is not possible to do with a textarea. What you are looking for is an content editable div, which is very easily done:

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

jsFiddle

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>

这篇关于在textarea中渲染HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆