开发者

Escaping ampersand in javascript/jquery

开发者 https://www.devze.com 2023-02-13 11:18 出处:网络
When I try to replace the string \"1/2\" with an HTML character with this line: $(this).text(($(this).text().replace(\'1/2\',\'½\')));

When I try to replace the string "1/2" with an HTML character with this line:

$(this).text(($(this).text().replace('1/2','½')));

it is instead replaced 开发者_C百科with ½

How can I escape the ampersand (&) in javascript.


If you want to deal in HTML, then just deal in HTML:

$(this).html(($(this).html().replace('1/2','½')));

That said, the escaped JavaScript representation of the character is "\u00BD", it should be simpler to just use "½" though.


Jquery.text() is used to insert & properly escape text. So It is normal that & becomes &.

To accomplish what you want to do use .html() instead of .text() in both occurences.

0

精彩评论

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