admin管理员组

文章数量:1415139

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 with ½

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

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 with ½

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

Share Improve this question edited Feb 27, 2011 at 15:52 Mike Samuel 121k30 gold badges227 silver badges254 bronze badges asked Feb 26, 2011 at 19:38 SaneefSaneef 8,9307 gold badges31 silver badges42 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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 & bees &.

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

本文标签: Escaping ampersand in javascriptjqueryStack Overflow