简体   繁体   中英

Use currency symbol with HTML5 and JavaScript

I am writing a basic HTML5 with some JavaScript.

I am using the following doctype:

<!DOCTYPE html>

And I have set my UTF-8 encoding like do (in the head tag):

<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">

The problem is, I have this javascript object which looks is exactly like this:

var symbol = {"GBP":"£", "EUR":"€", "USD":"$"};
alert(symbol['GBP']);

When the object is called it returns with the infamous !

Could somebody please point me in the right direction?

Thanks,

Peter

Some steps you don't mention:

  • Configure your editor to save files as UTF-8
  • Configure your web server to send Content-Type: text/html; charset=utf-8 Content-Type: text/html; charset=utf-8

What editor do you use? There is a possibility that you editor saves file in some iso8859 encoding. Try to use for example notepad and save file in UTF-8 encoding.

可能是您可以改用HTML实体。

var symbol = {"GBP":"&#163;", "EUR":"&#8364;", "USD":"&#36;"};

So it turns out, when you use the alert function, it doesn't like using special characters. My code was absolutely fine when I used the special characters in the HTML document. So instead I used something like this:

$(this).html(symbol['GPB']);

Thanks for your effort guys!

Peter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM