(非空)自关闭标签在 HTML5 中有效吗?

发布于 2024-09-15 10:33:39 字数 1217 浏览 8 评论 0 原文

W3C 验证器 (Wikipedia)在 非 void 元素。 (Void 元素 是那些可能不包含任何内容的元素.) 它们在 HTML5 中仍然有效吗?

接受 void 元素的一些示例:

<br />
<img src="" />
<input type="text" name="username" />

拒绝 非 void 元素的一些示例:

<div id="myDiv" />
<span id="mySpan" />
<textarea id="someTextMessage" />
Note:
The W3C validator actually accepts void self-closing tags: the author originally had a problem because of a simple typo (\> instead of />); however, self-closing tags are 100% not valid in HTML5 in general, and the answers elaborate on the issue of self-closing tags across various HTML flavors.

The W3C validator (Wikipedia) shows a warning for self-closing tags (those that end with “/>”) on non-void elements. (Void elements are those that may not ever contain any content.) Are they still valid in HTML5?

Some examples of accepted void elements:

<br />
<img src="" />
<input type="text" name="username" />

Some examples of rejected non-void elements:

<div id="myDiv" />
<span id="mySpan" />
<textarea id="someTextMessage" />


Note:
The W3C validator actually accepts void self-closing tags: the author originally had a problem because of a simple typo (\> instead of />); however, self-closing tags are 100% not valid in HTML5 in general, and the answers elaborate on the issue of self-closing tags across various HTML flavors.

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

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

发布评论

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

评论(8

貪欢 2024-09-22 10:33:39
  • (理论上)在 HTML 4 中, (是的,根本没有 >)意味着 < ;foo> (导致
    表示
    >
    (即
    > ;
    ) 和 表示 <code><title>hello)。我使用“理论上”这个术语是因为这是一个 SGML 规则,浏览器对它的支持非常差。支持很少(我只见过它在 emacs-w3m 中工作),< a href="http://www.w3.org/TR/html4/appendix/notes.html#hB.3.3" rel="noreferrer">规范建议作者避免使用语法。


  • XHTML中,> 表示。这是适用于所有 XML 文档的 XML 规则。也就是说,XHT​​ML 通常被用作 text/html ,(至少在历史上)浏览器使用与用作 application/xhtml+xml 的文档不同的解析器来处理它。 W3C 为 XHTML 作为 文本提供了要遵循的兼容性指南 /html. (本质上:仅当元素定义为 EMPTY 时才使用自闭合标记语法(并且 HTML 规范中禁止使用结束标记)。

  • HTML5中,的含义取决于元素的类型

    • 在指定为 void 元素 的 HTML 元素上(本质上是“HTML5 之前存在的元素,并且禁止包含任何内容”),结束标签是被禁止的。开始标记末尾的斜杠是允许的,但没有任何意义。对于沉迷于 XML 的人(和语法荧光笔)来说,它只是语法糖。
    • 在其他 HTML 元素上,斜杠为 错误,但错误恢复会导致浏览器忽略它,并将该标签视为常规开始标签。这通常会导致缺少结束标记,导致后续元素成为子元素而不是同级元素。
    • 外部元素(从 SVG 等 XML 应用程序导入)将其视为自闭合语法。
  • (Theoretically) in HTML 4, <foo / (yes, with no > at all) means <foo> (which leads to <br /> meaning <br>> (i.e. <br>>) and <title/hello/ meaning <title>hello</title>). I use the term "theoretically" because this is an SGML rule that browsers did a very poor job of supporting. There was so little support (I only ever saw it work in emacs-w3m) that the spec advises authors to avoid the syntax.

  • In XHTML, <foo /> means <foo></foo>. This is an XML rule that applies to all XML documents. That said, XHTML is often served as text/html which (historically at least) gets processed by browsers using a different parser than documents served as application/xhtml+xml. The W3C provides compatibility guidelines to follow for XHTML as text/html. (Essentially: Only use self-closing tag syntax when the element is defined as EMPTY (and the end tag was forbidden in the HTML spec)).

  • In HTML5, the meaning of <foo /> depends on the type of element:

    • On HTML elements that are designated as void elements (essentially "An element that existed before HTML5 and which was forbidden to have any content"), end tags are simply forbidden. The slash at the end of the start tag is allowed, but has no meaning. It is just syntactic sugar for people (and syntax highlighters) that are addicted to XML.
    • On other HTML elements, the slash is an error, but error recovery will cause browsers to ignore it and treat the tag as a regular start tag. This will usually end up with a missing end tag causing subsequent elements to be children instead of siblings.
    • Foreign elements (imported from XML applications such as SVG) treat it as self-closing syntax.
从﹋此江山别 2024-09-22 10:33:39

自关闭 div 不会验证。这是因为 div 是一个普通元素,而不是void 元素

根据 HTML5 规范,标签不能包含任何内容 (称为空元素)可以是自闭合的*。这包括以下标签:

area, base, br, col, embed, hr, img, input, 
link, meta, param, source, track, wbr

但是,上述标签中的“/”是完全可选的,因此 没有什么不同,但 无效。

*注意:外部元素也可以是自动关闭的,但我认为这不在这个答案的范围内。

A self-closing div will not validate. This is because a div is a normal element, not a void element.

According to the HTML5 spec, tags that cannot have any contents (known as void elements) can be self-closing*. This includes the following tags:

area, base, br, col, embed, hr, img, input, 
link, meta, param, source, track, wbr

The "/" is completely optional on the above tags, however, so <img/> is not different from <img>, but <img></img> is invalid.

*Note: foreign elements can also be self-closing, but I don't think that's in scope for this answer.

誰ツ都不明白 2024-09-22 10:33:39

实际上,在 HTML 中使用自关闭标签应该会像您期望的那样工作。但是,如果您担心编写有效 HTML5,那么您应该了解此类标记在您可以使用的两种不同语法形式中的行为方式。 HTML5 定义了 HTML 语法和 XHTML 语法,它们相似但不完全相同。使用哪一种取决于 Web 服务器发送的媒体类型。

您的页面很可能以 text/html 的形式提供,它遵循更宽松的 HTML 语法。在这些情况下,HTML5 允许某些开始标记在终止 > 之前有一个可选的 /。在这些情况下,/ 是可选的并被忽略,因此



是相同的。 HTML 规范将这些称为“空元素”,并给出了有效元素的列表。严格来说,可选的 / 仅在这些 void 元素的开始标签内有效;例如,


是有效的 HTML5,但

不是。

HTML5 规范明确区分了 HTML 作者和 Web 浏览器开发人员的正确内容,第二组需要接受各种无效的“旧版”语法。在这种情况下,这意味着兼容 HTML5 的浏览器将接受非法的自闭合标签,例如

,并按照您可能期望的方式呈现它们。但对于作者来说,该页面不是是有效的 HTML5。 (更重要的是,使用这种非法语法得到的 DOM 树可能会被严重搞砸;例如,自闭合 标签往往会把事情搞砸很多)。

(在特殊情况下,您的服务器知道如何将 XHTML 文件作为 XML MIME 类型发送,页面需要符合 XHTML DTD 和 XML 语法。这意味着这些文件需要自闭标签如此定义的元素。)

In practice, using self-closing tags in HTML should work just like you'd expect. But if you are concerned about writing valid HTML5, you should understand how the use of such tags behaves within the two different two syntax forms you can use. HTML5 defines both an HTML syntax and an XHTML syntax, which are similar but not identical. Which one is used depends on the media type sent by the web server.

More than likely, your pages are being served as text/html, which follows the more lenient HTML syntax. In these cases, HTML5 allows certain start tags to have an optional / before it's terminating >. In these cases, the / is optional and ignored, so <hr> and <hr /> are identical. The HTML spec calls these "void elements", and gives a list of valid ones. Strictly speaking, the optional / is only valid within the start tags of these void elements; for example, <br /> and <hr /> are valid HTML5, but <p /> is not.

The HTML5 spec makes a clear distinction between what is correct for HTML authors and for web browser developers, with the second group being required to accept all kinds of invalid "legacy" syntax. In this case, it means that HTML5-compliant browsers will accept illegal self-closed tags, like <p />, and render them as you probably expect. But for an author, that page would not be valid HTML5. (More importantly, the DOM tree you get from using this kind of illegal syntax can be seriously screwed up; self-closed <span /> tags, for example, tend to mess things up a lot).

(In the unusual case that your server knows how to send XHTML files as an XML MIME type, the page needs to conform to the XHTML DTD and XML syntax. That means self-closing tags are required for those elements defined as such.)

半暖夏伤 2024-09-22 10:33:39

HTML5 的行为基本上就好像尾随斜杠不存在一样。 HTML5 语法中不存在自闭合标签之类的东西。

  • 非 void 元素上的自闭合标签(例如

    )将不起作用根本不。尾部斜杠将被忽略,并且这些将被视为开始标签。这可能会导致嵌套问题。

    无论斜杠前面是否有空格,都是如此:

    也不起作用出于同样的原因。

  • void 元素上的自闭合标签,例如
    < /code> 工作,但只是因为尾部斜杠被忽略,在这种情况下,这恰好会导致正确的行为。


结果是,在旧的“XHTML 1.0 作为文本/html”中工作的任何内容都将继续像以前一样工作:非 void 标记上的尾部斜杠也不被接受,而 void 元素上的尾部斜杠则有效。

还有一点需要注意:可以将 HTML5 文档表示为 XML,这有时被称为“XHTML 5.0”。在这种情况下,将始终处理 XML 应用规则和自结束标记。它总是需要使用 XML mime 类型来提供服务。

HTML5 basically behaves as if the trailing slash is not there. There is no such thing as a self-closing tag in HTML5 syntax.

  • Self-closing tags on non-void elements like <p/>, <div/> will not work at all. The trailing slash will be ignored, and these will be treated as opening tags. This is likely to lead to nesting problems.

    This is true regardless of whether there is whitespace in front of the slash: <p /> and <div /> also won't work for the same reason.

  • Self-closing tags on void elements like <br/> or <img src="" alt=""/> will work, but only because the trailing slash is ignored, and in this case that happens to result in the correct behaviour.

The result is, anything that worked in your old "XHTML 1.0 served as text/html" will continue to work as it did before: trailing slashes on non-void tags were not accepted there either whereas the trailing slash on void elements worked.

One more note: it is possible to represent an HTML5 document as XML, and this is sometimes dubbed "XHTML 5.0". In this case the rules of XML apply and self-closing tags will always be handled. It would always need to be served with an XML mime type.

三生池水覆流年 2024-09-22 10:33:39

自关闭标签在 HTML5 中有效,但不是必需的。



都很好。

Self-closing tags are valid in HTML5, but not required.

<br> and <br /> are both fine.

权谋诡计 2024-09-22 10:33:39

我会非常小心地使用自闭合标签,如下例所示:

var a = '<span/><span/>';
var d = document.createElement('div');
d.innerHTML = a
console.log(d.innerHTML) // "<span><span></span></span>"

我的直觉是

I would be very careful with self closing tags as this example demonstrates:

var a = '<span/><span/>';
var d = document.createElement('div');
d.innerHTML = a
console.log(d.innerHTML) // "<span><span></span></span>"

My gut feeling would have been <span></span><span></span> instead

我是有多爱你 2024-09-22 10:33:39

然而 - 只是为了记录 - 这是无效的:

<address class="vcard">
  <svg viewBox="0 0 800 400">
    <rect width="800" height="400" fill="#000">
  </svg>
</address>

这里的斜杠将使其再次有效:

    <rect width="800" height="400" fill="#000"/>

However -just for the record- this is invalid:

<address class="vcard">
  <svg viewBox="0 0 800 400">
    <rect width="800" height="400" fill="#000">
  </svg>
</address>

And a slash here would make it valid again:

    <rect width="800" height="400" fill="#000"/>
裂开嘴轻声笑有多痛 2024-09-22 10:33:39

(非空)自关闭标签在 HTML5 中有效吗?

当然,它们是有效的,但几乎没有修改。

以自闭合标签
为例。

即使您编写

,它们最终也会在中转换为
浏览器。

在以 /> /> 结尾的自闭合标签中,/(斜线)和空格将被忽略。

举个例子,让我们看看它在浏览器中的样子。

<p>This is paragraph with <br><br> and <br/><br/> and then <br /><br />.</p>

上面的代码在浏览器中将如下图所示。

输入图片这里的描述

可以看到全部转换为
。因此,您可以选择是否关闭自关闭标签,但它们是完全有效的。

Are (non-void) self-closing tags valid in HTML5?

Of course, they are valid but with little modification.

Take an example a self-closing tag <br>.

Even if you write <br/> or <br /> they will eventually be converted to <br> in the browser.

In self-closing tags ending with /> or />, / (slash) and white space will simply be ignored.

Take an example and let's see how it looks in the browser.

<p>This is paragraph with <br><br> and <br/><br/> and then <br /><br />.</p>

The above code will look like the following image in the browser.

enter image description here

You can see all converted to <br>. So it's your choice to close the self-closing tag or not but they are completely valid.

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