var temp = document.createElement("div")
(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html)
var output = temp.innerHTML
temp = null
return output
}
var tagText = "<p><b>123&456</b></p>"
console.log(HTMLEncode(tagText))//<p><b>123&456</b></p>
function HTMLDecode(text) {
var temp = document.createElement("div")
temp.innerHTML = text
var output = temp.innerText || temp.textContent
temp = null
return output
}
var tagText = "<p><b>123&456</b></p>"
var encodeText=HTMLEncode(tagText)
console.log(encodeText) //<p><b>123&456</b></p>
console.log(HTMLDecode(encodeText)) //<p><b>123&456</b></p>
html的右斜杠转义符号是“\\”。最常用的字符实体
显示结果 描述 实体名称 实体编号
空格
<小于号< <
> 大于号> >
& 和号& &
" 引号""
' 撇号' (IE不支持)'
其他一些常用的字符实体
¢ 分¢¢
£ 镑££
¥ 日圆¥¥
§ 节§§
©版权©©
® 注册商标®®
×乘号××
÷ 除号÷
asp用Server.HTMLEncode<%response.write(Server.HTMLEncode("The image tag: <img>"))%>
javascript 用正则表达式进行转换处理
var HtmlUtil = {
/*1.用正则表达式实现html转码*/
htmlEncodeByRegExp:function (str){
var s = ""
if(str.length == 0) return ""
s = str.replace(/&/g,"&")
s = s.replace(/</g,"<")
s = s.replace(/>/g,">")
s = s.replace(/ /g," ")
s = s.replace(/\'/g,"'")
s = s.replace(/\"/g,""")
return s
},
/*2.用正则表达式实现html解码*/
htmlDecodeByRegExp:function (str){
var s = ""
if(str.length == 0) return ""
s = str.replace(/&/g,"&")
s = s.replace(/</g,"<")
s = s.replace(/>/g,">")
s = s.replace(/ /g," ")
s = s.replace(/'/g,"\'")
s = s.replace(/"/g,"\"")
return s
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)