HTML 5中是不是不需要像HTML那样关闭标签呢?

发布于 2024-12-04 01:32:29 字数 508 浏览 1 评论 0原文

HTML 5中是不是不需要像HTML那样关闭标签呢?或者它是 W3C 验证器中的错误

为什么此代码在 W3C 验证器中有效

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>
  <body>
<p>Some Text
  </body>
</html>

如果它在 HTML5 中确实有效,我会感到惊讶。但是在 HTML5 中保持这种行为有效有什么好处吗? HTML5 创建者是否认为更严格的 XHTML 规则对 Web 不利?

Is it not necessary to close the tag in HTML 5 like HTML? or it's a bug in W3C validator

Why this code is valid in W3C validator

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>
  <body>
<p>Some Text
  </body>
</html>

I would be surprised if it's really valid in HTML5. But is there any benefit to keep this behavior valid in HTML5. Do HTML5 creators think that stricter rules of XHTML were not good for Web?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哥,最终变帅啦 2024-12-11 01:32:29

该标记确实有效。

标签不必关闭在 HTML 4.01 或 HTML5 中。 我不确定您从哪里得到 HTML5 要求所有内容都像 XHTML 一样关闭的想法。

HTML5 只是带有额外新功能的常规 HTML(因此版本从 4.01 跃升至 5)。它绝不以任何方式派生自 XHTML。您可以关闭所有 HTML5 标记,使其看起来像格式良好的 XML,但规范并不要求您这样做。

That markup is indeed valid. <p> tags don't have to be closed in HTML 4.01 or HTML5. I'm not sure where you got the idea that HTML5 requires everything to be closed like in XHTML.

HTML5 is just regular HTML with extra new features (hence the version jump from 4.01 to 5). It does not in any way derive from XHTML. You can close all of your HTML5 tags so it looks like well-formed XML, but the spec doesn't require you to.

GRAY°灰色天空 2024-12-11 01:32:29

HTML5 创建者是否认为更严格的 XHTML 规则对 XHTML 不利?
网络?

差不多,是的。

他们的观点是,这只会让创建网页变得更加困难。 HTML 取得了巨大的成功,因为几乎任何人都可以创建一个可用的网页,而无需了解任何 HTML。这是一个非常小的入门学习曲线,人们可以在准备好后继续使用。

如果你需要了解很多迂腐的规则才能开始,那么很多人就不会费心,HTML 也不会那么成功。

Do HTML5 creators think that stricter rules of XHTML were not good for
Web?

Pretty much, yes.

Their view is that it just makes creating a web page harder. HTML has been wildly successful because just about anyone can create a working web page without knowing barely any HTML at all. It's a very small learning curve to get started which people can build upon when they're ready.

If you need to know a lot of pedantic rules just to get started, then many people won't bother, and HTML will not be as successful.

鲸落 2024-12-11 01:32:29

在大多数情况下,保留

元素的结束标记是有效的,但也有少数情况是无效的。 万维网联盟的具体规则是:

如果 p 元素后面紧跟着 地址文章旁白,< a href="https://www.w3.org/TR/html-markup/blockquote.html#blockquote" rel="nofollow noreferrer">块引用, dir, div, dl, 字段集页脚, 表单, h1, h2, h3h4, h5, h6, 标题hr菜单, nav, ol, p, pre, 部分表格 或 ul 元素,或者如果没有父元素中的更多内容和父元素不是 a 元素。

因此,例如,以下内容是无效的:

<a href="http://example.com><p>This paragraph is unclosed</a>

但这是有效的:

<div class="news"><p>Something important happened!</div>

HTML 从来不需要关闭

标记 - 它始终是可选的。您可以关闭 HTML 标记,使其看起来像格式良好的 XHTML,但这不是必需的。 XHTML 比 HTML 更严格。

Leaving the closing tag for a <p> element out is valid in most situations, though there are a few where it isn't. The exact rules at the World Wide Web Consortium are:

A p element’s end tag may be omitted if the p element is immediately followed by an address, article, aside, blockquote, dir, div, dl, fieldset, footer, form, h1, h2, h3, h4, h5, h6, header, hr, menu, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is not an a element.

So, for example, the following is invalid:

<a href="http://example.com><p>This paragraph is unclosed</a>

But this is valid:

<div class="news"><p>Something important happened!</div>

HTML never required the <p> tag to be closed—it was always optional. You may close your HTML tags so it looks like well-formed XHTML, but that isn't necessary. XHTML is more strict than HTML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文