开发者

jQuery JavaScript Encoding HTML

开发者 https://www.devze.com 2023-01-19 02:24 出处:网络
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.

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);
0

精彩评论

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