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.
精彩评论