javascript 在 textarea 中显示 html 输出而不是 HTML 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19441026/
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
show html output in textarea instead of HTML code
提问by user2888402
If I have a textarea including some HTML code, how can I write a JavaScript function to show the HTML output instead of HTML code itself, for example:
如果我有一个包含一些 HTML 代码的 textarea,我如何编写一个 JavaScript 函数来显示 HTML 输出而不是 HTML 代码本身,例如:
<textarea id="mytextarea">
<table border=1><tr><td>cellone</td>td>celltwo</td></tr></table
</textarea>
<input type=button onclick="HTMLoutput();"/>
<script>
HTMLoutput()
{
//Code to show html output instead of html code in textarea
}
</script>
How can I do this? What is the suggested code to write inside the HTMLoutput()?
我怎样才能做到这一点?在 HTMLoutput() 中编写的建议代码是什么?
采纳答案by Claudio Santos
So to convert the the html code to a formated html you need to do:
因此,要将 html 代码转换为格式化的 html,您需要执行以下操作:
$('resultDiv').append($('<div/>').html($('.txtArea').val()+"<br>");
here's an examplethat use div with contentEditable set to true.
回答by SLaks
It sounds like you're asking how to take some HTML and display its rendered result in your document.
听起来您在询问如何获取一些 HTML 并在您的文档中显示其呈现的结果。
That's exactly what the innerHTML
property does.
Simply pick a DOM element to display the result in, and set its innerHTML
to the HTML to display.
这正是innerHTML
物业所做的。
只需选择一个 DOM 元素来显示结果,并将其设置innerHTML
为要显示的 HTML。