javascript 是否可以使用 createTextNode 方法来呈现 html 标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6965498/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:30:08 来源:igfitidea点击:
Is it possible to get the createTextNode method to render html tags?
提问by user784637
The following code prints
以下代码打印
This should print(b)This should print(/b)This should print
这应该打印(b) 这应该打印 (/b)这应该打印
<script>
function produceMessage(){
var msg= '<b>This should print</b>';
return msg;
}
</script>
<span id="mySpan"></span>
<script>
document.body.appendChild(document.createTextNode(produceMessage()));
document.write(produceMessage());
document.getElementById('mySpan').innerHTML=produceMessage();
</script>
回答by Digital Plane
No, a text node will not print any HTML. Instead, create an element, or use a document fragment to insert HTML in that way.
不,文本节点不会打印任何 HTML。相反,创建一个元素,或使用文档片段以这种方式插入 HTML。
function boldHTML() {
var element = document.createElement("b");
element.innerHTML = "Bold text";
return element;
}
document.body.appendChild(boldHTML());
will print Bold text.
将打印粗体文本。