18

这些问题说明了一切:)

例如。我们有>,我们只需要>使用javascript

更新:似乎 jquery 是最简单的出路。但是,有一个轻量级的解决方案会很好。更像是一个能够自行执行此操作的函数。

4

5 回答 5

30

你可以这样做:

String.prototype.decodeHTML = function() {
    var map = {"gt":">" /* , … */};
    return this.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);?/gi, function($0, $1) {
        if ($1[0] === "#") {
            return String.fromCharCode($1[1].toLowerCase() === "x" ? parseInt($1.substr(2), 16)  : parseInt($1.substr(1), 10));
        } else {
            return map.hasOwnProperty($1) ? map[$1] : $0;
        }
    });
};
于 2010-12-02T19:40:42.420 回答
20
function decodeEntities(s){
    var str, temp= document.createElement('p');
    temp.innerHTML= s;
    str= temp.textContent || temp.innerText;
    temp=null;
    return str;
}

alert(decodeEntities('<'))

/*  returned value: (String)
<
*/
于 2010-12-02T19:46:38.557 回答
3

这是一个用于解码整个 HTML 文档的“类”。

HTMLDecoder = {
    tempElement: document.createElement('span'),
    decode: function(html) {
        var _self = this;
        html.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);/gi,
            function(str) {
                _self.tempElement.innerHTML= str;
                str = _self.tempElement.textContent || _self.tempElement.innerText;
                return str;
            }
        );
    }
}

请注意,我使用 Gumbo 的正则表达式来捕获实体,但对于完全有效的 HTML 文档(或 XHTML),您可以简单地使用/&[^;]+;/g.

于 2014-11-05T10:39:24.100 回答
1

没有内置任何内容,但是已经编写了许多库来执行此操作。

是一个。

这里是一个 jQuery 插件

于 2010-12-02T19:32:05.747 回答
1

我知道那里有图书馆,但这里有一些针对浏览器的解决方案。这些在将 html 实体数据字符串放置到您希望显示字符的人工可编辑区域时效果很好,例如 textarea 或 input[type=text]。

我添加了这个答案,因为我必须支持旧版本的 IE,我觉得它结束了几天的研究和测试。我希望有人觉得这很有用。

首先,这是针对使用 jQuery 的更现代的浏览器,请注意,如果您必须支持 10(7、8 或 9)之前的 IE 版本,则不应使用它,因为它会删除换行符,只留下一条长线的文本。

if (!String.prototype.HTMLDecode) {
    String.prototype.HTMLDecode = function () {
            var str = this.toString(),
            $decoderEl = $('<textarea />');

        str = $decoderEl.html(str)
            .text()
            .replace(/<br((\/)|( \/))?>/gi, "\r\n");

        $decoderEl.remove();

        return str;
    };
}

下一个是基于 kennebec 的上述工作,有一些差异主要是为了旧的 IE 版本。这不需要 jQuery,但仍然需要浏览器。

if (!String.prototype.HTMLDecode) {
    String.prototype.HTMLDecode = function () {
        var str = this.toString(),
            //Create an element for decoding            
            decoderEl = document.createElement('p');

        //Bail if empty, otherwise IE7 will return undefined when 
        //OR-ing the 2 empty strings from innerText and textContent
        if (str.length == 0) {
            return str;
        }

        //convert newlines to <br's> to save them
        str = str.replace(/((\r\n)|(\r)|(\n))/gi, " <br/>");            

        decoderEl.innerHTML = str;
        /*
        We use innerText first as IE strips newlines out with textContent.
        There is said to be a performance hit for this, but sometimes
        correctness of data (keeping newlines) must take precedence.
        */
        str = decoderEl.innerText || decoderEl.textContent;

        //clean up the decoding element
        decoderEl = null;

        //replace back in the newlines
        return str.replace(/<br((\/)|( \/))?>/gi, "\r\n");
    };
}

/* 
Usage: 
    var str = "&gt;";
    return str.HTMLDecode();

returned value: 
    (String) >    
*/
于 2014-07-24T22:53:33.900 回答