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

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

我知道 XHTML 在技术上允许任何元素自封闭,但我正在寻找所有主要浏览器支持的那些元素的列表。见 http://dusan.fora.si/blog/self-closing-tags以

等自闭合元素引起的一些问题为例。

最佳答案

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

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

DOCTYPE 不会改变文档的解释方式。 Only MIME type does

W3C decision about ignoring DOCTYPE :

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 在很大程度上忽略了该规则,但浏览器却虔诚地遵循它。读 Understanding HTML, XML and XHTML来自 WebKit 博客:

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,请将其放入您的文档中:

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

它会验证,并且在真正的 XHTML 中它可以完美运行(参见:1 与 2 )。如果您不相信自己的眼睛(或不知道如何设置 MIME 类型),请通过 XHTML proxy 打开您的页面.

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

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


作为记录,XHTML 规范允许任何元素通过将 XHTML 设为 XML application 来自动关闭。 : [强调我的]

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

它也在 XHTML spec 中明确显示:

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

https://stackoverflow.com/questions/97522/

相关文章:

html - 在 Notepad++ 中格式化代码

email - 使电子邮件地址免受网页上的机器人攻击?

html - 标签中是否需要 ="text/css"类型?

javascript - 从 onclick/onchange 事件中获取 HTML Checkbo

html - 如何保存 Chrome 开发者工具样式面板的 CSS 更改?

html - 在 HTML 中呈现字符串并保留空格和换行符

html - 如何用固定宽度换行 HTML 按钮的文本?

javascript - 如何使用 JavaScript 获取 HTML 页面的标题?

html - 设置表格列宽

html - 并排的两个 div - 流体显示