Skip to content

Instantly share code, notes, and snippets.

@vivisidea
Created November 16, 2012 08:44
Show Gist options
  • Save vivisidea/4085559 to your computer and use it in GitHub Desktop.
Save vivisidea/4085559 to your computer and use it in GitHub Desktop.
转换HTML特殊字符
function HtmlEncode(s){
var el = document.createElement("div");
el.innerText = el.textContent = s;
s = el.innerHTML;
return s;
}
// Test run:
// alert(HtmlEncode('&;\'><"'));
// jquery: $('<div>').text(value.word).html()
// reference: http://stackoverflow.com/questions/784586/convert-special-characters-to-html-in-javascript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment