(HTML5)使用UTF-8时无法显示汉字 [英] (HTML5) cannot display Chinese characters when using UTF-8
问题描述
我正在尝试使用HTML5在我的网页上显示一些汉字.
I am trying to display some Chinese characters on my web page using HTML5.
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
// ...
context.fillText('中文', 0, 0);
context.fill();
我在这里检查了几个问题,并在标题中添加了以下内容.
And I have checked several questions here and added the following in the header.
<meta charset="UTF-8" />
我在Firefox 14中尝试过,它在屏幕上显示????
.当我意识到这一点时,我在浏览器中设置的字符编码为UTF-8
.当我将其更改为"Big5"时,它显示得很好.
I tried in Firefox 14, and it shows ????
on the screen. While I realized that, my browser setting of character encoding is UTF-8
. When I changed it to 'Big5`, it displays well.
我的问题是,该脚本可以允许浏览器在不更改特定字符编码的情况下正确显示中文(也许所有其他非英语)字符吗?也就是说,将设置保留为UTF-8
,那么用户浏览器就不会再有其他处理了,因为我希望页面显示多种语言.
My question is, can the script allow browser to display Chinese (and perhaps all other non-English) characters properly without changing to the specific character encoding? i.e. keeping the setting as UTF-8
and then nothing deal with the user's browsers furthermore because I want my page to display more than one language.
谢谢.
推荐答案
您似乎已将源代码保存在Big5中.您应该
You seem to have saved your source code in Big5. You should either
- 将您的源代码另存为UTF-8,或者
- 对非ASCII字符使用Unicode转义码.
如果您更喜欢使用转义码,则您的示例可以写为:
If you prefer to use escape codes, your example can be written as:
context.fillText('\u4e2d\u6587', 0, 0);
这篇关于(HTML5)使用UTF-8时无法显示汉字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!