Escaping ampersand in javascriptjquery - Stack Overflow

When I try to replace the string "12" with an HTML character with this line: $(this).text(($

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745222743a4617324.html

相关推荐

  • Escaping ampersand in javascriptjquery - Stack Overflow

    When I try to replace the string "12" with an HTML character with this line: $(this).text(($

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信