jQuery对html进行Encode和Decode

时间:2023-03-08 16:06:10

最近需要在前台对编辑器生成的html代码做处理,需要进行编码,考虑到js没有直接对html编码的支持,看了下jQuery的实现,发现真是超级简单呀,顺便记录一下,需要的时候可以参考一下。

function htmlEncode(value){
if (value) {
return jQuery('<div />').text(value).html();
} else {
return '';
}
} function htmlDecode(value) {
if (value) {
return $('<div />').html(value).text();
} else {
return '';
}
}