How to insert HTML entities with createTextNode?

后端 未结 3 489
执念已碎
执念已碎 2020-12-06 01:52

If I want to add an ascii symbol form js to a node somewhere? Tried as a TextNode, but it didn\'t parse it as a code:

var dropdownTriggerText =          


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-06 02:28

    createTextNode is supposed to take any text input and insert it into the DOM exactly like it is. This makes it impossible to insert for example HTML elements, and HTML entities. It’s actually a feature, so you don’t need to escape these first. Instead you just operate on the DOM to insert text nodes.

    So, you can actually just use the & symbol directly:

    var dropdownTriggerText = document.createTextNode('blabla &');
    

提交回复
热议问题