admin管理员组

文章数量:1317906

I am returning special characters (specifically °) in JavaScript/jQuery, but it is not converting the entity to the desired character in the display. How can I display the degree sign correctly?

It must be simple; what am I missing? I have checked decodeURL, but it does nothing.

Demo fiddle

The HTML:

<p>Try to give answer is: </p>
<div id="target"></div>

<p>But should look like:</p>
<div> 5 &deg;C</div>

And the Javascript with jQuery:

$('#target').text('5 &deg;C');

Output:

I am returning special characters (specifically &deg;) in JavaScript/jQuery, but it is not converting the entity to the desired character in the display. How can I display the degree sign correctly?

It must be simple; what am I missing? I have checked decodeURL, but it does nothing.

Demo fiddle

The HTML:

<p>Try to give answer is: </p>
<div id="target"></div>

<p>But should look like:</p>
<div> 5 &deg;C</div>

And the Javascript with jQuery:

$('#target').text('5 &deg;C');

Output:

Share Improve this question edited Jan 29, 2021 at 2:17 Abel Callejo 15k12 gold badges76 silver badges92 bronze badges asked May 1, 2015 at 16:05 SablefosteSablefoste 4,1923 gold badges40 silver badges60 bronze badges 1
  • possible duplicate of Unescape HTML entities in Javascript? – user1596138 Commented May 1, 2015 at 16:15
Add a ment  | 

2 Answers 2

Reset to default 8

To see the interpreted character entity you need to use html():

$('#target').html('5 &deg;C');

Updated fiddle

Try this:

$('#target').html('5 &deg;C');

text function escapes the html characters so you get what you see.

本文标签: javascriptAppending a special character with jQuery not workingStack Overflow