HTML Encode 和Decode

mac2022-06-30  122

在使用富文本编辑器来保存数据时,传到后台的数据是以带标签的html文本来保存的,这时候需要用到html的解码和加码来作中间的桥梁来转换数据。下面是前台JS

的加解码方法:

function HTMLEncode(html) { var temp = document.createElement("div"); (temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html); var output = temp.innerHTML; temp = null; return output; } function HTMLDecode(text) { var temp = document.createElement("div"); temp.innerHTML = text; var output = temp.innerText || temp.textContent; temp = null; return output; }

同时推荐一个比较好用的富文本编辑器(wangEditor),这是一个在github上开源的插件,界面简洁,功能完整同时还有全套的中文参考Doc。

 

转载于:https://www.cnblogs.com/Aaron-Lee/p/9468205.html

最新回复(0)