我应该删除元标记中的尾部斜杠吗?
W3C 标记验证 要求我删除页面上的尾部斜杠。
这是正确的吗?我的页面在所有浏览器中仍然兼容吗?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="content-style-type" content="text/css" />
<!-- ^ -->
W3C markup validation is asking me to remove trailing slashes on my page.
Is this correct? And will my page still be compliant in all browsers?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="content-style-type" content="text/css" />
<!-- ^ -->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是正确的。对于 HTML 4.01,
meta
元素不应该有尾部斜杠(自闭合标签),如果我没记错的话,我相信元素。
不过,xhtml 需要尾部斜杠。
That is correct. For HTML 4.01 you shouldn't have trailing slashes (self-closing tags) for
meta
elements and I believe<link>
elements if memory serves me correctly.xhtml requires trailing slashes, though.
XHTML
是具有XML
语法的HTML
。在 XML 中,所有标签都必须闭合。某些 HTML 标签没有结束标签,因此在 XHTML 中它们需要自闭合。这是通过在>
前面放置/
来实现的。因此,必须更改
/>
中的任何标记以删除正斜杠,以符合 HTML 要求。这些并不是两个规格之间的唯一差异。
XHTML
isHTML
with anXML
syntax. In XML all tags must be closed. Some HTML tags do not have closing tags so in XHTML they need to be self closed. This is achieved through placing an/
in front of the>
.So any tags that you have with
/>
must be changed to remove the forward slash to be HTML conformant.These aren't the only differences between the two specifications.
问题的答案是肯定的,你应该删除多余的固相线。 (如果您愿意,也可以称为尾部斜杠)。
HTML5 也是如此。 HTML 不需要在结束符前使用斜杠 >字符(又名“自闭合”标签,也是用词不当),例如
和
、
、
等,并且由于多种原因不鼓励这种使用,如所提供的超链接的目标中所讨论的。注意:最好始终参考官方 W3C 或 WHATWG 资源中的 HTML 规范。 (例如 https://html.spec.whatwg.org/multipage/#toc-语义)有关历史的更多信息,请参见此处:
https://www.cnet.com/culture/forming-pains -afflict-html5-standardization/
尾部斜杠是为了促进 XHTML 中的 XML“兼容性”,并且通常仅在使用 XHTML 严格文档类型时用户代理才会考虑。 (例如,Transitional 无论如何都只是将其解析为 HTML)
该语法仍然被广泛错误地使用(例如,WordPress 据称是 2023 年网络上使用的最流行的内容管理系统。查看大多数 WP 网站的源代码,您会看到它在 HTML 中使用了不正确的语法,但值得注意的是代码格式化应用程序 Prettier 至少有部分责任)
The answer to the question is Yes, you should remove the superfluous solidus. (aka trailing slash, if you wish).
This is also true of HTML5. HTML does not require the slash before the closing > character (aka. "self-closing" tags, also a misnomer), such as
<link />
, and<meta />
,<br />
,<hr />
, etc, and such use is is discouraged for multiple reasons as discussed at the target of the hyperlink provided. Note: it is always best to reference the HTML Specification at either an official W3C or WHATWG resource. (e.g. https://html.spec.whatwg.org/multipage/#toc-semantics )More info about the history, here:
https://www.cnet.com/culture/growing-pains-afflict-html5-standardization/
The Trailing slash was to promote XML "compatibility" in XHTML, and was typically only considered by the User Agent ONLY if the XHTML Strict Doctype was used. (e.g. Transitional just parsed it as HTML anyway)
The syntax is still widely used incorrectly (e.g. WordPress is allegedly the most popular content management system in use on the web in 2023. View the source of most any WP site, and you'll see it uses incorrect syntax in the HTML. however, it's worth noting that the code-formatting app, Prettier is at least partly to blame)
这里有一个用例。
标记不得有尾部斜杠。
我有 5 个不同的网站,想将它们从搜索引擎结果中删除。
我设置了元标记
,结果,其中 4 个从 Bing 和 Google 搜索结果中完全消失了。但是,仍然只出现一页。
我最终找到了原因。
唯一仍然显示的页面在元标记中具有尾部斜杠,其他 4 个页面没有。
One usecase here. The
<meta>
tag must not have the trailing slash.I have 5 different websites and want to remove them from search engine results.
I set the meta tag
As a result, 4 of them completely disappeared from Bing and Google search results. But, only one page still appears.
I eventually found out the reason.
The only still-appearing page had the trailing slash in the meta tag, the 4 others didn't have.