javascript 我如何在 JS 中取消转义 HTML 实体?(将 < 改为 <)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4480757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:50:16  来源:igfitidea点击:

How do i unescape HTML Entities in JS? (change &lt; to <)

javascripthtml

提问by Sky Sanders

How do i unescape HTML Entities in JS?

我如何在 JS 中取消转义 HTML 实体?

When googling i literally saw answers with a huge switch and people rolling their own.

在谷歌搜索时,我确实看到了一个巨大的开关和人们自己滚动的答案。

I'd like the string &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;to become <html xmlns="http://www.w3.org/1999/xhtml" >

我希望字符串&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;变成<html xmlns="http://www.w3.org/1999/xhtml" >

回答by Sky Sanders

Create a div, set it's innerHTMLand then read innerText

创建一个div,设置它innerHTML然后读取innerText

var d = document.createElement("div");
d.innerHTML = "&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;";
alert(d.innerText || d.text || d.textContent);

回答by Samuel

Here is a JS implementation of a PHP function that decodes HTML entities

这是一个解码 HTML 实体的 PHP 函数的 JS 实现

http://phpjs.org/functions/html_entity_decode

http://phpjs.org/functions/html_entity_decode

Note that this function also depends on:

请注意,此功能还取决于:

http://phpjs.org/functions/get_html_translation_table

http://phpjs.org/functions/get_html_translation_table