后台构建 html 字符串传到前台字符串转码(html)处理_html/css_WEB-ITnose
程序员文章站
2022-05-08 21:17:14
...
知识在于总结,那就记下了吧!
例如后台 html 字符串是
var htmlStr="后台html字符串";
转码
var html格式代码=decodeHtml(htmlStr);
只需调用下面这个js方法就行了
function decodeHtml(s) { var HTML_DECODE = { "<": "", "&": "&", " ": " ", """: "\"", "©": "" // Add more }; var REGX_HTML_ENCODE = /"|&|'||[\x00-\x20]|[\x7F-\xFF]|[\u0100-\u2700]/g; var REGX_HTML_DECODE = /&\w+;|(\d+);/g; var REGX_TRIM = /(^\s*)|(\s*$)/g; s = (s != undefined) ? s : ""; return (typeof s != "string") ? s : s.replace(REGX_HTML_DECODE, function ($0, $1) { var c = HTML_DECODE[$0]; if (c == undefined) { // Maybe is Entity Number if (!isNaN($1)) { c = String.fromCharCode(($1 == 160) ? 32 : $1); } else { c = $0; } } return c; }); };