在textarea中渲染HTML

The*_*onk 134 html javascript jquery textarea

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

mek*_*all 219

这是不可能的textarea.您正在寻找的是内容可编辑的 div,这很容易完成:

<div contenteditable="true"></div>
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

div.editable {
    width: 300px;
    height: 200px;
    border: 1px solid #ccc;
    padding: 5px;
}

strong {
  font-weight: bold;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

  • 如果不使用javascript来使用ajax提交更改或将它们复制到表单输入中,您实际上无法使用此实际的耻辱.如果有人找到了办法,我真的很感兴趣! (8认同)
  • 这不比`textarea`更好.在小提琴中输入"<strong> someting </ strong>"会给你留下那些确切的文字.HTML未应用. (7认同)
  • @yckart:嗯,我不认为这实际上是有效的,即使它适用于我尝试过的所有浏览器.[`contenteditable`是枚举属性](http://www.w3.org/TR/html5/editing.html#contenteditable)而不是布尔属性(有一个`inherit`状态以及`true`和` false`),我*认为*这意味着指定一个值是强制性的,虽然我找不到规范的确定位来确认这一点.MDN [似乎同意](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes#contenteditable). (3认同)
  • @yckart:我假设浏览器将`contenteditable`解释为没有值,因为它与`contenteditable =""`相同,而`contenteditable ="true"`. (2认同)
  • @MarcusEkwall但不是要求html渲染的问题? (2认同)

Sam*_*age 31

使用可编辑的div,您可以使用该方法document.execCommand(更多详细信息)轻松地为您指定的标记和其他一些功能提供支持.

#text {
    width : 500px;
	min-height : 100px;
	border : 2px solid;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

  • 这在某些浏览器中不起作用,只需将其添加为 '16 的注释即可 (2认同)
  • 看起来它已被弃用 - 真遗憾 (2认同)

mer*_*lin 5

因为您只说过渲染,所以可以。您可以按照以下方式进行操作:

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"));
 }
Run Code Online (Sandbox Code Playgroud)
#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;
}
Run Code Online (Sandbox Code Playgroud)
<textarea id="box" oninput="render()">you can edit me!</textarea>
Run Code Online (Sandbox Code Playgroud)
这样就textarea可以渲染HTML!除了在调整大小时闪烁外,还不能直接使用类,并且必须确保div中的div 与插入符号的svg格式相同,textarea以便正确对齐,这是可行的!