jQuery JavaScript Encoding HTML
How on earth do you encode HTML with jQuery/JavaScript?
Havn't found any working solution after hours of Googling 开发者_Go百科and surfing this site.
This:
jQuery('<div/>').text(value).html();
does not work with jQuery!
What do you mean with "encode"?
If you mean something similar to PHP's htmlentities, your solution should work with jQuery:
http://jsfiddle.net/wrH8b/
What you have works, you can test it here, make sure you're using that to get the encoded text.
If your goal is to encode it for transmission (e.g. a POST), you should be looking at encodeURIComponent()
instead, for example:
var encodedString = encodeURIComponent(value);
精彩评论