简体   繁体   English

在HTML5和JavaScript中使用货币符号

[英]Use currency symbol with HTML5 and JavaScript

I am writing a basic HTML5 with some JavaScript. 我正在用一些JavaScript编写基本的HTML5。

I am using the following doctype: 我正在使用以下文档类型:

<!DOCTYPE html>

And I have set my UTF-8 encoding like do (in the head tag): 我已经将我的UTF-8编码设置为do(在head标签中):

<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: 问题是,我有一个看起来像这样的javascript对象:

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 配置您的编辑器以将文件另存为UTF-8
  • Configure your web server to send Content-Type: text/html; charset=utf-8 配置您的Web服务器以发送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. 您的编辑器可能会以某种iso8859编码保存文件。 Try to use for example notepad and save file in UTF-8 encoding. 尝试使用记事本为例,并以UTF-8编码保存文件。

可能是您可以改用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. 当我在HTML文档中使用特殊字符时,我的代码绝对正确。 So instead I used something like this: 所以我改用这样的东西:

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

Thanks for your effort guys! 谢谢你们的努力!

Peter 彼得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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