javascript; JS版HtmlEncode方法,结果与C#中HttpUtility.HtmlEncode方法一样。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript; JS版HtmlEncode方法,结果与C#中HttpUtility.HtmlEncode方法一样。相关的知识,希望对你有一定的参考价值。

<script type="text/javascript"> 
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; 
} 

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

var html = "<br>dffdfqqqqq"; 
var encodeHTML = HTMLEncode(html); 
alert("方法一:" +encodeHTML); 
var decodeHTML = HTMLDecode(encodeHTML); 
alert("方法一:" +decodeHTML); 
</script> 

以上是关于javascript; JS版HtmlEncode方法,结果与C#中HttpUtility.HtmlEncode方法一样。的主要内容,如果未能解决你的问题,请参考以下文章