js实现encodeHTML和decodeHTML
2020-12-24 01:32
435 查看
encodeHTMl
const encodeHTML = (html: string): string => { const div = document.createElement('div'); div.textContent = html; return div.innerHTML; } const decodeHTML = (html: string): string => { const div = document.createElement('div'); div.innerHTML = html; return div.innerText || div.innerHTML; } const html = '<p>html decode/encode test &</p>'; let encodedHTML = encodeHTML(html); let decodedHTML = decodeHTML(encodedHTML); console.log(`raw html: ${html}`); console.log(`encoded html: ${encodedHTML}`); console.log(`decoded html: ${decodedHTML}`); console.log(`&<p>aaa</p>`)
HTML编码(encodeHTMl)
对HTML编码就是对HTML文档中的特殊字符进行编码, 使得浏览器不会将这些内容识别为HTML文档内容;
会变编码的字符如
- &:
&
- <:
<
- >:
>
HTML解码(decodeHTML)
HTML编码的逆过程, 将HTML中进行过编码的字符恢复为原来的字符
&
: &<
: <>
: >
相关文章推荐
- 用js进行htmlencode与decode
- 兼容IE和FF的JS HTMLEncode和HTMLDecode的完整实例[转]
- JS 实现php中的 BASE64_ENCODE 和 BASE64_DECODE
- 解决JSP_url参数中文乱码及用JS_javascirpt编码js实现encode后台JSP_解码decode的实现
- javascript实现htmlEncode与htmlDecode
- JS 实现BASE64_ENCODE和BASE64_DECODE(实例代码)
- jquery 高效实现htmlencode()与htmldecode()函数
- 用Javascript实现HtmlEncode与HtmlDecode的另类方法
- JS中 HTMLEncode和HTMLDecode
- js实现C#的Server.Encode和Server.Decode
- HTML链接上把当前的URL做为参数传递时,注意要对URL进行encode,在JS中我们这样实现
- javascript另类方法高效实现htmlencode()与htmldecode()函数
- php实现JS的encodeURI和decodeURI
- 兼容IE和FF的JS HTMLEncode和HTMLDecode的完整实例
- js实现C#的Server.Encode和Server.Decode
- JS 实现BASE64_ENCODE和BASE64_DECODE(实例代码)
- js之html encode,decode
- javascript 实现htmlEncode htmlDecode
- js实现C#的Server.Encode和Server.Decode
- javascript另类方法高效实现htmlencode()与htmldecode()函数