Javascript / jQuery - convert special html characters

后端 未结 2 1946
南笙
南笙 2021-02-09 12:15

I have a pre element with some html code in it. the code has special characters in it, like <, so it doesn\'t break the page.

Then I hav

2条回答
  •  失恋的感觉
    2021-02-09 12:34

    I don't know what happens (and why it happens) to your html, but you can use jQuerys .text() and .html() to decode/encode html entitiys like:

    HTML

    <<

    jQuery:

    var t = $('#test');
    t.html(t.text()); // will print "<<"
    

    example: http://www.jsfiddle.net/fphw3

    update

    Since you mentioned that you use .html() to read the value of your element, a call to .text() instead should solve your issue.

提交回复
热议问题