开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜