为什么我们需要 HTML head 中的元内容类型标签?
为什么我们需要像这样在 HTML head 中包含元内容类型标签?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
Why do we need to include the meta content type tag in HTML head like this?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每当在本地(从磁盘文件系统)打开网页时,
text/html
部分将指示网页浏览器该文档的类型,以便它知道如何解析它以及字符集=UTF-8
将指示网络浏览器应使用哪种字符编码来显示网页上的字符,以便它不会使用平台默认编码(这可能不一定是正确的编码,因此可能会结束为 mojibake)。重要的细节是,当页面通过 HTTP 通过网络提供服务时,不会使用此 HTML 元标头。相反,将使用 HTTP 响应标头中的
Content-Type
。因此,如果缺少charset=UTF-8
部分,而内容实际上被解码为 UTF-8,那么它很可能会变成 mojibake。您可以使用 Firebug 来确定Content-Type
标头,Net< /em> 面板。Whenever the webpage is been opened locally (from disk file system), the
text/html
part will instruct the webbrowser of which type the document is so that it knows how to parse it and thecharset=UTF-8
will instruct the webbrowser which character encoding should be used to display the characters on the webpage so that it won't use the platform default encoding (which may not necessarily be the right one which would thus potentially end up as mojibake).Important detail is that this HTML meta header isn't been used when the page is served over the web by HTTP. Instead, the
Content-Type
one in the HTTP response header will be used. So if this lacks thecharset=UTF-8
part while the content is actually decoded as UTF-8, then it will likely go mojibake. You can determine theContent-Type
header with for example Firebug, in the Net panel.元元素通常用于指定页面描述、关键字、文档作者、最后修改时间和其他元数据。
您发布的元标记将指示浏览器具有
text/html
类型的文档,其字符集或语言设置为UTF-8
。更多信息,请参阅此:
Meta elements are typically used to specify page description, keywords, author of the document, last modified, and other metadata.
The meta tag you have posted, will instruct the browser to have
text/html
type of document with character set or language set toUTF-8
.See this for more info:
这样浏览器就知道如何解码页面 - 例如:根据语言的不同,最终结果可能会有很大差异。
So that the browser would know how to decode the page - for example: depending on the language, the end-result may differ a lot.