HtmlSpecialChars equivalent in Javascript?

后端 未结 16 1758
耶瑟儿~
耶瑟儿~ 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:36

    With jQuery it can be like this:

    var escapedValue = $('
    ').text(value).html();

    From related question Escaping HTML strings with jQuery

    As mentioned in comment double quotes and single quotes are left as-is for this implementation. That means this solution should not be used if you need to make element attribute as a raw html string.

提交回复
热议问题