知识在于总结,那就记下了吧!
例如后台 html 字符串是
var htmlStr="后台html字符串";
转码
var html格式代码=decodeHtml(htmlStr);
只需调用下面这个js方法就行了
function decodeHtml(s) { var HTML_DECODE = { "<": "<", ">": ">", "&": "&", " ": " ", """: """, "©": ""<span style="color: #008000;">//</span><span style="color: #008000;"> Add more</span>
};
</span><span style="color: #0000ff;">var</span> REGX_HTML_ENCODE = /"|&|'|<|>|[x00-x20]|[x7F-xFF]|[u0100-u2700]/<span style="color: #000000;">g; </span><span style="color: #0000ff;">var</span> REGX_HTML_DECODE = /&w+;|&#(d+);/<span style="color: #000000;">g; </span><span style="color: #0000ff;">var</span> REGX_TRIM = /(^s*)|(s*$)/<span style="color: #000000;">g; s </span>= (s != undefined) ? s : ""<span style="color: #000000;">; </span><span style="color: #0000ff;">return</span> (<span style="color: #0000ff;">typeof</span> s != "string") ?<span style="color: #000000;"> s : s.replace(REGX_HTML_DECODE, </span><span style="color: #0000ff;">function</span> ($0, $1<span style="color: #000000;">) { </span><span style="color: #0000ff;">var</span> c = HTML_DECODE[$0<span style="color: #000000;">]; </span><span style="color: #0000ff;">if</span> (c ==<span style="color: #000000;"> undefined) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> Maybe is Entity Number</span> <span style="color: #0000ff;">if</span> (!isNaN($1<span style="color: #000000;">)) { c </span>= String.fromCharCode(($1 == 160) ? 32 : $1<span style="color: #000000;">); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { c </span>= $0<span style="color: #000000;">; } } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> c; }); };</span></pre>