Convert HTML Character Entities back to regular text using javascript

后端 未结 5 2070
梦毁少年i
梦毁少年i 2020-11-29 08:54

the questions says it all :)

eg. we have >, we need > using only javascript

Update: It seems jquery is t

5条回答
  •  盖世英雄少女心
    2020-11-29 09:12

    I know there are libraries out there, but here are a couple of solutions for browsers. These work well when placing html entity data strings into human editable areas where you want the characters to be shown, such as textarea's or input[type=text].

    I add this answer as I have to support older versions of IE and I feel that it wraps up a few days worth of research and testing. I hope somebody finds this useful.

    First this is for more modern browsers using jQuery, Please note that this should NOT be used if you have to support versions of IE before 10 (7, 8, or 9) as it will strip out the newlines leaving you with just one long line of text.

    if (!String.prototype.HTMLDecode) {
        String.prototype.HTMLDecode = function () {
                var str = this.toString(),
                $decoderEl = $('