将文本转换为html javascript

时间:2014-04-02 22:53:21

标签: javascript angularjs

我的html内容包含特殊字符

<div class="agfMailBodyHtml"><font size=2 face="sans-serif">Test mail 5 - reply op rich text with <b>attachements\r\n</b>also with <b>attchements</b></font>\r\n<br>\r\n<br><font size=2 face="sans-serif"><br>\r\n</font>\r\n<br>\r\n<br>\r\n<br>\r\n<br><font size=1 color=#5f5f5f face="sans-serif">

是否可以使用javascript将其转换为html?

2 个答案:

答案 0 :(得分:2)

var el = document.createElement('div');
el.innerHTML = "<div class="agfMailBodyHtml"><font size=2 face="sans-serif">Test mail 5 - reply op rich text with <b>attachements\r\n</b>also with <b>attchements</b></font>\r\n<br>\r\n<br><font size=2 face="sans-serif"><br>\r\n</font>\r\n<br>\r\n<br>\r\n<br>\r\n<br><font size=1 color=#5f5f5f face="sans-serif">";
var html = el.textContent;

答案 1 :(得分:2)

是的,有可能:

var str = "<div class="agfMailBodyHtml"><font size=2 face="sans-serif">Test mail 5 - reply op rich text with <b>attachements\r\n</b>also with <b>attchements</b></font>\r\n<br>\r\n<br><font size=2 face="sans-serif"><br>\r\n</font>\r\n<br>\r\n<br>\r\n<br>\r\n<br><font size=1 color=#5f5f5f face="sans-serif">";

var div = document.createElement("div");
div.innerHTML = str;

div.innerHTML = div.textContent;
alert(div.innerHTML);
document.body.appendChild(div);

DEMO FIDDLE