How to insert HTML as Text

前端 未结 5 1663
无人及你
无人及你 2020-12-09 20:03

I want HTML, for example,

, to show show as just that, in plain text, and not interpreted by the browser as an actual tag.

I know JQuery has .h

5条回答
  •  时光取名叫无心
    2020-12-09 20:43

    There's no need to escape the characters. Simply use createTextNode:

    var text = document.createTextNode('

    Stuff

    '); document.body.appendChild(text);

    See a working example here: http://jsfiddle.net/tZ3Xj/.

    This is exactly how jQuery does it (line 43 of jQuery 1.5.2):

    return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
    

提交回复
热议问题