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 badges2 Answers
Reset to default 4If 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745222743a4617324.html
评论列表(0条)