开发者

Is there any method to unescape '>' to '>' in JavaScript?

开发者 https://www.devze.com 2022-12-27 04:33 出处:网络
i want to escape some HTML in Ja开发者_StackOverflow中文版vaScript.How can I do that?I often use the following function to decode HTML Entities:

i want to escape some HTML in Ja开发者_StackOverflow中文版vaScript. How can I do that?


I often use the following function to decode HTML Entities:

function htmlDecode(input){
  var e = document.createElement('div');
  e.innerHTML = input;
  return e.childNodes[0].nodeValue;
}

htmlDecode('&lt;&gt;'); // "<>"

Simple, cross-browser and works with all the HTML 4 Character Entities.


You could create a dummy textarea, set its innerHTML to your escaped html [the html with &gt;s] and use the textarea.value

var ta = document.createElement('textarea');
 ta.innerHTML = "&gt;";
 alert(ta.value);

... had to use this on a CMS once [although when i used it, it was bad practice]

0

精彩评论

暂无评论...
验证码 换一张
取 消