简体   繁体   English

HTML-使用UTF-8或ISO-8859-1时,我仍然需要键入特殊字符的代码吗?

[英]HTML - When using UTF-8 or ISO-8859-1 do I still need to type the codes for the special characters?

It is as the title says: 如标题所示:

HTML - When using UTF-8 or ISO-8859-1 do I still need to type the codes for the special characters? HTML-使用UTF-8或ISO-8859-1时,我仍然需要键入特殊字符的代码吗?

Or can I just type them normally? 还是我可以正常键入它们?

Ex: I'm using UTF-8 in my HTML META tag. 例如:我在HTML META标签中使用UTF-8。 I need to type ç should I just type it or type its code which is ç 我需要输入ç还是输入它的代码还是ç代码ç

I know this is a trivial question, but it's fundamental so I just can't skip it. 我知道这是一个琐碎的问题,但这是基本问题,因此我无法跳过。

No, you only need to use a character reference if: 不,您仅在以下情况下需要使用字符引用:

  • The character you want cannot be represented in the character encoding you are using or 您想要的字符无法用您正在使用的字符编码
  • The character has some special meaning in HTML (such as < or & ). 该字符在HTML中具有某些特殊含义(例如<& )。

Note that declaring you are using UTF-8 in the meta tag is insufficient. 请注意,声明在meta标签中使用UTF-8是不够的。 You also have to encode the HTML source in UTF-8 (good editors will default to this) and not override it with a declaration of some other encoding in the real HTTP headers. 您还必须使用UTF-8对HTML源进行编码(好的编辑器将默认使用该格式),并且不能在真实的HTTP标头中用其他编码的声明覆盖它。 You should also set the real HTTP headers to state that UTF-8 is being used. 您还应该设置真实的HTTP标头,以声明正在使用UTF-8。

Yes, you can include those characters directly in your HTML source, without using the entity for the character. 是的,您可以直接在HTML源代码中包含这些字符,而无需使用该字符的实体。 Just make sure that the encoding you are saving the file in really does match what the web server serves it in. 只要确保您保存文件的编码确实与Web服务器提供的编码匹配即可。

The part about ensuring that the encoding is correct is important, and easy to get wrong. 确保编码正确的部分很重要,而且很容易出错。 One thing to note is that the meta tag is not the primary source of information that the browser uses for interpreting the encoding of the document. 需要注意的一件事是, meta标签不是浏览器用来解释文档编码的主要信息源。 The primary source of information is the Content-type header, sent as part of the HTTP headers. 信息的主要来源是作为HTTP标头一部分发送的Content-type标头。 The meta tag was originally supposed to be used to communicate to the web server what Content-type to use, but most web servers use configuration separate from the document itself for this. 最初应将meta标记用于与Web服务器通信要使用的Content-type ,但是大多数Web服务器为此使用与文档本身不同的配置。 So if you are saving your document as UTF-8, make sure that the web server is configured to serve pages as UTF-8 as well. 因此,如果将文档另存为UTF-8,请确保将Web服务器配置为也将页面用作UTF-8。

The meta tag is used by browsers as a fallback if the Content-type header is not provided or does not include valid encoding information. 如果未提供Content-type标头或不包含有效的编码信息,则浏览器会将meta标记用作备用。 It is useful to have if you are ever going to be loading from a source that doesn't provide Content-type information, like using a file: URL to view the page on your local machine. 如果要从不提供“ Content-type信息的源进行加载,例如使用file: URL在本地计算机上查看页面,则很有用。

So, there are 3 places you should make sure your encoding is set up properly; 因此,您应该在3个地方确保正确设置了编码; in your text editor (so that it saves the file with the appropriate encoding), in your web server configuration (so that it communicates the appropriate encoding to the browser), and in the meta tag, so that when you view the page locally, it is displayed with the correct encoding. 在您的文本编辑器中(以便它以适当的编码保存文件),在Web服务器配置中(以使其将适当的编码传达给浏览器)以及在meta标签中,这样当您在本地查看页面时,它以正确的编码显示。

Finally, you shouldn't use ISO-8859-1. 最后,您不应该使用ISO-8859-1。 That's a legacy encoding, only still supported for compatibility. 这是一种旧式编码,仅出于兼容性考虑仍受支持。 Every major browser and text editor supports UTF-8 by now, which covers all of Unicode, and provides a lot fewer encoding headaches. 现在,每个主要的浏览器和文本编辑器都支持UTF-8,它涵盖了所有Unicode,并且减少了很多编码难题。

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

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