且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JS中取消对HTML实体的转义? (将& lt;更改为<)

更新时间:2022-12-04 21:12:50

创建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);

How do i unescape HTML Entities in JS?

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" >

Create a div, set it's innerHTML and then read 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);