js如何转义和反转义html特殊字符

 时间:2026-05-04 23:57:04

1、//HTML转义 

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;}

2、//HTML反转义

 function HTMLDecode(text){   var temp = document.createElement(“div”);   temp.innerHTML = text;   var output = temp.innerText || temp.textContent;   temp = null;   return output;}

3、对于HTML反转义还有一种方法,让html自己来解析再取值,原理其实跟上面的一样,不过有这是直接写到页面上。

方法:在页面上写个隐藏的<div id=”decode” style=”display:none;”>未反转义的html内容</div>来放未反转义的html,再用js来获取html。 $(“#decode”).html();

  • 如何建立Apache的访问控制
  • 如何使用ES6中的Proxy设置和获取对象的属性
  • weblogic控制台添加oracle RAC数据源
  • 手机UC浏览器如何设置标识状态为手机版?
  • 怎样在无域名解析情况下的访问自己的网站?
  • 热门搜索
    旅顺旅游 东戴河旅游景区 庐山旅游景点 湄洲岛旅游攻略 绵山旅游攻略 安徽黄山旅游 南戴河旅游 陕西旅游烹饪职业学院 新西兰旅游签证 旅游景区质量等级的划分与评定