手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

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();

© 手抄报圈