(非空)自闭合标签在 HTML5 中有效吗? [英] Are (non-void) self-closing tags valid in HTML5?

查看:27
本文介绍了(非空)自闭合标签在 HTML5 中有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

W3C 验证器(维基百科)不喜欢 非空 元素.(空元素是那些可能永远不包含任何内容的元素.) 它们在 HTML5 中仍然有效吗?

一些接受无效元素的例子:

<br/><img src=""/><输入类型=文本"名称=用户名"/>

一些被拒绝非空元素的例子:

<子>注意:
W3C 验证器实际上接受 void 自闭合标签:作者最初因为一个简单的错字(> 而不是 />)而遇到问题;然而,一般来说,自闭合标签在 HTML5 中并不是 100% 有效,答案详细说明了各种 HTML 风格的自闭合标签问题.

解决方案

  • (理论上)在 HTML 4 中,(是的,根本没有 >)意味着 <foo>(这导致 <br/> 意思是
    >
    (即
    &gt;
    ) 和 意思是 <code><title>hello).我使用术语理论上"因为这是一个SGML 规则,浏览器在支持方面做得很差.支持太少(我只看到它在 emacs-w3m 中工作)以至于 规范建议作者避免使用这种语法.

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

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

    • 在指定为 void 元素的 HTML 元素上(本质上是一个在 HTML5 之前就存在并且被禁止包含任何内容的元素"),只是简单地禁止结束标签.开始标记末尾的斜线是允许的,但没有意义.对于沉迷于 XML 的人(和语法高亮者)来说,这只是语法糖.
    • 在其他 HTML 元素上,斜线是 一个错误,但错误恢复将导致浏览器忽略它并将该标签视为常规开始标签.这通常会导致缺少结束标记,导致后续元素成为子元素而不是兄弟元素.
    • 外部元素(从 SVG 等 XML 应用程序导入)将其视为自闭合语法.

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>&gt;) 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆