XHTML 中所有有效的自闭合元素(由主要浏览器实现)是什么?

2022-01-30 00:00:00 browser html xhtml cross-browser

XHTML 中所有有效的自闭合元素(例如 <br/>)是什么(由主流浏览器实现)?

What are all the valid self-closing elements (e.g. <br/>) in XHTML (as implemented by the major browsers)?

我知道 XHTML 在技术上允许任何元素自封闭,但我正在寻找所有主要浏览器支持的那些元素的列表.请参阅 http://dusan.fora.si/blog/self-closing-tags 举一些自闭元素引起的问题的例子,例如<div/>.

I know that XHTML technically allows any element to be self-closed, but I'm looking for a list of those elements supported by all major browsers. See http://dusan.fora.si/blog/self-closing-tags for examples of some problems caused by self-closing elements such as <div />.

推荐答案

所有支持 XHTML 的浏览器(Firefox、Opera、Safari、IE9) 支持每个元素上的自闭合语法.

Every browser that supports XHTML (Firefox, Opera, Safari, IE9) supports self-closing syntax on every element.

<div/>, <script/>, <br></br> 都应该工作正好.如果没有,那么您的 HTML 中添加了不当的 XHTML DOCTYPE.

<div/>, <script/>, <br></br> all should work just fine. If they don't, then you have HTML with inappropriately added XHTML DOCTYPE.

DOCTYPE 不会改变文档的解释方式.只有 MIME 类型可以.

W3C 关于忽略 DOCTYPE 的决定:

HTML WG 讨论了这个问题:其目的是允许旧的(仅 HTML)浏览器接受 XHTML 1.0 文档,遵循指南,并将它们作为 text/html 提供.因此,文件作为text/html 应被视为 HTML 而不是 XHTML.

The HTML WG has discussed this issue: the intention was to allow old (HTML-only) browsers to accept XHTML 1.0 documents by following the guidelines, and serving them as text/html. Therefore, documents served as text/html should be treated as HTML and not as XHTML.

这是一个非常常见的陷阱,因为 W3C Validator 在很大程度上忽略了该规则,但浏览器却虔诚地遵循它.读理解 HTML、XML 和 XHTML 来自 WebKit 博客:

It's a very common pitfall, because W3C Validator largely ignores that rule, but browsers follow it religiously. Read Understanding HTML, XML and XHTML from WebKit blog:

事实上,互联网上绝大多数所谓的 XHTML 文档都是以 text/html 的形式提供的.这意味着它们根本不是 XHTML,而是通过 HTML 解析器的错误处理得到的实际上无效的 HTML.所有那些有效的 XHTML 1.0!"网络上的链接实际上是在说无效的 HTML 4.01!".

In fact, the vast majority of supposedly XHTML documents on the internet are served as text/html. Which means they are not XHTML at all, but actually invalid HTML that’s getting by on the error handling of HTML parsers. All those "Valid XHTML 1.0!" links on the web are really saying "Invalid HTML 4.01!".


要使用 XHTML 的 DOCTYPE 测试您是否有真正的 XHTML 或无效的 HTML,请将其放入您的文档中:


To test whether you have real XHTML or invalid HTML with XHTML's DOCTYPE, put this in your document:

<span style="color:green"><span style="color:red"/> 
 If it's red, it's HTML. Green is XHTML.
</span>

它验证,并且在真正的 XHTML 中它完美地工作(参见:1 vs 2).如果您不相信自己的眼睛(或不知道如何设置 MIME 类型),请通过 XHTML 代理打开您的页面.

It validates, and in real XHTML it works perfectly (see: 1 vs 2). If you can't believe your eyes (or don't know how to set MIME types), open your page via XHTML proxy.

另一种检查方法是在 Firefox 中查看源代码.当斜线无效时,它将以红色突出显示.

Another way to check is view source in Firefox. It will highlight slashes in red when they're invalid.

在 HTML5/XHTML5 中这并没有改变,而且区别更加清晰,因为你甚至没有额外的 DOCTYPE.Content-Type为王.

In HTML5/XHTML5 this hasn't changed, and the distinction is even clearer, because you don't even have additional DOCTYPE. Content-Type is the king.

作为记录,XHTML 规范允许任何元素通过使 XHTML 成为 XML 应用程序:[强调我的]

For the record, the XHTML spec allows any element to be self-closing by making XHTML an XML application: [emphasis mine]

空元素标签可用于任何没有内容的元素,无论它是否使用关键字 EMPTY 声明.

Empty-element tags may be used for any element which has no content, whether or not it is declared using the keyword EMPTY.

XHTML 规范中也明确显示:

空元素必须要么有结束标记,或者开始标记必须以/>结尾.例如,<br/><hr></hr>

Empty elements must either have an end tag or the start tag must end with />. For instance, <br/> or <hr></hr>

相关文章