在 HTML 中显示 unicode 符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1696619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:14:14  来源:igfitidea点击:

Displaying unicode symbols in HTML

htmlunicode

提问by Peter Craig

I want to simply display the tick (?) and cross (?) symbols in a HTML page but it shows up as either a box or goop a?” - obviously something to do with the encoding.

我只想在 HTML 页面中显示勾号 (?) 和叉号 (?) 符号,但它显示为一个框或一个 goop a?- 显然与编码有关。

I have set the meta tag to show utf-8 but obviously I'm missing something.

我已将元标记设置为显示 utf-8,但显然我遗漏了一些东西。

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

Edit/Solution:From comments made, using FireBug I found the headers being passed by my page were in fact "Content-Type: text/html" and not UTF-8. Looking at the file format using Notepad++ showed my file was formatted as "UTF-8 without BOM". Changing this to just UTF-8 the symbols now show correctly... but firebug still seems to indicate the same content-type.

编辑/解决方案:从评论中,使用 FireBug 我发现我的页面传递的标题实际上是“内容类型:文本/html”而不是 UTF-8。使用 Notepad++ 查看文件格式显示我的文件格式为“UTF-8 without BOM”。将其更改为仅 UTF-8 符号现在可以正确显示......但萤火虫似乎仍然表示相同的内容类型。

采纳答案by Peter Craig

You should ensure the HTTP server headers are correct.

您应该确保 HTTP 服务器标头正确无误。

In particular, the header:

特别是,标题:

Content-Type: text/html; charset=utf-8

should be present.

应该存在。

The meta tag is ignored by browsers if the HTTP header is present.

如果存在 HTTP 标头,则浏览器会忽略元标记。

Also ensure that your file is actually encoded as UTF-8 before serving it, check/try the following:

还要确保您的文件在提供之前实际编码为 UTF-8,请检查/尝试以下操作:

  • Ensure your editor save it as UTF-8.
  • Ensure your FTP or any file transfer program does not mess with the file.
  • Try with HTML encoded entities, like &#uuu;.
  • To be really sure, hexdump the file and look as the character, for the ?, it should be E2 9C 94 .
  • 确保您的编辑器将其保存为 UTF-8。
  • 确保您的 FTP 或任何文件传输程序不会弄乱文件。
  • 尝试使用 HTML 编码的实体,例如&#uuu;.
  • 可以肯定的是,hexdump 文件并查看字符,对于 ?,它应该是 E2 9C 94 。

Note: If you use an unicode character for which your system can't find a glyph (no font with that character), your browser should display a question mark or some block like symbol. But if you see multiple roman characters like you do, this denotes an encoding problem.

注意:如果您使用系统找不到字形的 unicode 字符(没有带有该字符的字体),您的浏览器应该显示一个问号或一些块状符号。但是,如果您像您一样看到多个罗马字符,则表示存在编码问题。

回答by Akshay

I know an answer has already been accepted, but wanted to point a few things out.

我知道一个答案已经被接受,但想指出一些事情。

Setting the content-typeand charsetis obviously a good practice, doing it on the server is much better, because it ensures consistency across your application.

设置content-typecharset显然是一个很好的做法,在服务器上设置要好得多,因为它确保了整个应用程序的一致性。

However, I would use UTF-8only when the language of my application uses a lot of characters that are available only in the UTF-8charset. If you want to show a unicode character or symbol in one of cases, you can do so without changing the charsetof your page.

但是,我UTF-8只会在我的应用程序语言使用大量仅在UTF-8字符集中可用的字符时使用。如果您想在其中一种情况下显示 unicode 字符或符号,则无需更改charset页面的 即可。

HTMLrenderers have always been able to display symbols which are not part of the encoding character set of the page, as long as you mention the symbol in its numeric character reference (NCR). Sounds weird but its true.

HTML渲染器始终能够显示不属于页面编码字符集的符号,只要您在其numeric character reference (NCR). 听起来很奇怪,但这是真的。

So, even if your htmlhas a header that states it has an encoding of ansior any of the isocharsets, you can display a check mark by using its html character reference, in decimal - &#10003; or in hex - &#x2713;

因此,即使您html的标头表明它具有编码ansi或任何iso字符集,您也可以通过使用其 html 字符引用(十进制 - ✓)来显示复选标记。或十六进制 - ✓

So its a little difficult to understand why you are facing this issue on your pages. Can you check if the NCR value is correct, this is a good reference http://www.fileformat.info/info/unicode/char/2713/index.htm

所以有点难以理解为什么你在你的页面上面临这个问题。你能检查一下NCR值是否正确,这是一个很好的参考http://www.fileformat.info/info/unicode/char/2713/index.htm

回答by Guffa

Make sure that you actually save the file as UTF-8, alternatively use HTML entities (&#nnn;) for the special characters.

确保您实际将文件保存为 UTF-8,或者使用 HTML 实体 ( &#nnn;) 作为特殊字符。

回答by Konrad Rudolph

Unlike proposed by Nicolas, the metatag isn't actually ignored by the browsers. However, the Content-TypeHTTP header always has precedence over the presence of a metatag in the document.

与 Nicolas 提出的不同,该meta标签实际上并未被浏览器忽略。但是,Content-TypeHTTP 标头始终优先meta于文档中存在的标记。

So make sure that you either send the correct encoding via the HTTP header, or don't send this HTTP header at all (not recommended). The metatag is mainly a fallback option for local documents which aren't sent via HTTP traffic.

因此,请确保您要么通过 HTTP 标头发送正确的编码,要么根本不发送此 HTTP 标头(不推荐)。该meta标签主要是不通过 HTTP 流量发送的本地文档的后备选项。

Using HTML entities should also be considered a workaround – that's tiptoeing around the real problem. Configuring the web server properly prevents a lot of nuisance.

使用 HTML 实体也应该被视为一种变通方法——这是绕着真正的问题踮起脚尖。正确配置网络服务器可以避免很多麻烦。

回答by Kugel

I think this is a file problem, you simple saved your file in 1-byte encoding like latin-1. Google up your editor and how to set files to utf-8.

我认为这是一个文件问题,您只需将文件保存为 1 字节编码,如 latin-1。谷歌搜索你的编辑器以及如何将文件设置为 utf-8。

I wonder why there are editors that don't default to utf-8.

我想知道为什么有些编辑器不默认为 utf-8。