Is it possible to get the createTextNode method to render html tags?
The following code prints
This should print(b)This should print(/b)This should print
<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.getEl开发者_Go百科ementById('mySpan').innerHTML=produceMessage();
</script>
No, a text node will not print any HTML. Instead, create an element, or use a document fragment to insert HTML in that way.
function boldHTML() {
var element = document.createElement("b");
element.innerHTML = "Bold text";
return element;
}
document.body.appendChild(boldHTML());
will print Bold text.
精彩评论