HTML实体的编码和解码

<script type="text/javascript">

//编码:document.getElementById("test").innerText = encode("

test

");//<p>test</p>

 

function encode(input){
  var converter = document.createElement("div");
  converter.innerText = input;
  var output = converter.innerHTML;
  converter = null;
  return output;
 }

 

解码:document.getElementById("test").innerText = decode("<p>test</p>");//

test


function decode(input){
  var converter = document.createElement("div");
  converter.innerHTML = input;
  var output = converter.innerText;
  converter = null;
  return output;

}

//在XHTML文档中使用CDATA避免解析HTML实体
//<![CDATA[
   document.write(6>5);

//]]>
script>

你可能感兴趣的:(JavaScript)