How to insert HTML entities with createTextNode?

后端 未结 3 494
执念已碎
执念已碎 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:34

    I couldn't find an automated way to do this. So I made a function.

    // render HTML as text for inserting into text nodes
    function renderHTML(txt) {
      var tmpDiv = document.createElement("div"); tmpDiv.innerHTML = txt;
      return tmpDiv.innerText || tmpDiv.textContent || txt;
    }
    

提交回复
热议问题