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