开发者

Escaping ampersand in javascript/jquery

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜