HtmlSpecialChars equivalent in Javascript?

后端 未结 16 1739
耶瑟儿~
耶瑟儿~ 2020-11-22 06:00

Apparently, this is harder to find than I thought it would be. And it even is so simple...

Is there a function equivalent to PHP\'s htmlspecialchars built into Javas

16条回答
  •  逝去的感伤
    2020-11-22 06:28

    That's HTML Encoding. There's no native javascript function to do that, but you can google and get some nicely done up ones.

    E.g. http://sanzon.wordpress.com/2008/05/01/neat-little-html-encoding-trick-in-javascript/

    EDIT:
    This is what I've tested:

    var div = document.createElement('div');
      var text = document.createTextNode('');
      div.appendChild(text);
      console.log(div.innerHTML);
    

    Output: <htmltag/>

提交回复
热议问题