HTML5中有(非无效)自闭标签是否有效? [英] Are (non-void) self-closing tags valid in HTML5?

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

问题描述

W3C验证器不喜欢自闭标签(以 /> 非空洞元素。 (void元素是那些可能永远不会包含任何内容的元素)它们仍然在HTML5中有效吗?


$ b 接受 void元素的一些示例:

< br />
< img src =/>
< input type =textname =username/>

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

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

注意: validator实际上接受void self-closing标签:作者最初由于简单的打字错误( \> 而不是 /> )。 然而,自我关闭标签在HTML5中通常不是100%有效,而且这些答案详细讨论了各种HTML风格的自闭标签问题。

HTML 4 中,< foo / $ c>(是,没有> )意味着< foo> (这导致< br /> 含义< br>> (ie < br>& amp ;> )和< title / hello / 含义< title> hello< / title> )。浏览器在支持这方面做得很差,规范建议作者避免语法


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

  • HTML5 < foo /> 取决于元素的类型


    • 被指定为 void元素(实质上是HTML5之前存在并且被禁止拥有任何内容的元素),则简单地禁止结束标记。开始标签末尾的斜线是允许的,但没有意义。它仅仅是人们使用的语法糖(和语法荧光笔),它们会沉迷于XML。

    • 在其他HTML元素上,斜杠是一个错误,但错误恢复将导致浏览器忽略它并将该标记视为常规开始标记。这通常会以缺失的结束标记结束,导致后续元素成为孩子而不是同胞。

    • 外部元素(从XML应用程序(如SVG导入)将其视为自闭合语法。



    The W3C validator 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.

    解决方案

    • 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>). Browsers did a very poor job of supporting this and 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天全站免登陆