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

 时间:2024-10-14 00:48:28

1、//HTML转义functionHTMLEncode(html) { vartemp=document.cr髫潋啜缅eateElement(“div”); (temp.textContent!=null)?(temp.textContent=html):(temp.innerText=html); varoutput=temp.innerHTML; temp=null; returnoutput; }

2、//HTML反转义functionHTMLDecode(text) { vartemp=document.createElement(“div”); temp.innerHTML=text; varoutput=temp.innerText||temp.textContent; temp=null; returnoutput; }

3、对于HTML反转义还有一种方法,让html自己来解析再取值,原理其实跟上面的一样,不过有这是直接写到页面上。方法:在页面上写个隐藏的<div id=”decode” style=”display:none;”>未反转义的html内容</div>来放未反转义的html,再用js来获取html。 $(“#decode”).html();

  • 如何建立Apache的访问控制
  • 如何使用ES6中的Proxy设置和获取对象的属性
  • weblogic控制台添加oracle RAC数据源
  • 手机UC浏览器如何设置标识状态为手机版?
  • 热门搜索
    病人祝福语 豁然开朗是什么意思 风轻云淡的意思 s档是什么意思 420是什么意思 research是什么意思 奶粉一段二段什么意思 皮条客是什么意思 香港买什么 稀奇古怪的意思