(非空)自闭合标签在 HTML5 中有效吗? [英] Are (non-void) self-closing tags valid in HTML5?
问题描述
W3C 验证器(维基百科)不喜欢 非空 元素.(空元素是那些可能永远不包含任何内容的元素.) 它们在 HTML5 中仍然有效吗?
一些接受无效元素的例子:
<br/><img src=""/><输入类型=文本"名称=用户名"/>
一些被拒绝非空元素的例子:
<子>注意:
W3C 验证器实际上接受 void 自闭合标签:作者最初因为一个简单的错字(>
而不是 />
)而遇到问题;然而,一般来说,自闭合标签在 HTML5 中并不是 100% 有效,答案详细说明了各种 HTML 风格的自闭合标签问题.
解决方案 (理论上)在 HTML 4 中,(是的,根本没有 >
)意味着 <foo>
(这导致 <br/>
意思是
>
(即
>
) 和 意思是 hello
).我使用术语理论上"因为这是一个SGML 规则,浏览器在支持方面做得很差.支持太少(我只看到它在 emacs-w3m 中工作)以至于 规范建议作者避免使用这种语法.
在 XHTML 中,<foo/>
表示
.这是适用于所有 XML 文档的 XML 规则.也就是说,XHTML 通常作为 text/html
使用(至少在历史上)由浏览器使用与作为 application/xhtml+xml
的文档不同的解析器来处理.W3C 为 XHTML 提供了兼容性指南作为text/html
.(本质上:仅当元素定义为 EMPTY(并且 HTML 规范中禁止使用结束标记)时才使用自关闭标记语法).
在HTML5中,
的含义取决于元素的类型:
The W3C validator (Wikipedia) doesn't like 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 not 100% valid in HTML5 in general, and the answers elaborate on the issue of self-closing tags across various HTML flavors.
解决方案
(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.
这篇关于(非空)自闭合标签在 HTML5 中有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文