Html XHTML 中所有有效的自关闭元素是什么(由主要浏览器实现)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/97522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are all the valid self-closing elements in XHTML (as implemented by the major browsers)?
提问by kamens
What are all the valid self-closing elements (e.g. <br/>) in XHTML (as implemented by the major browsers)?
XHTML(由主要浏览器实现)中所有有效的自关闭元素(例如 <br/>)是什么?
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-tagsfor examples of some problems caused by self-closing elements such as <div />.
我知道 XHTML 在技术上允许任何元素自我关闭,但我正在寻找所有主要浏览器支持的那些元素的列表。有关由 <div /> 等自闭合元素引起的一些问题的示例,请参见http://dusan.fora.si/blog/self-closed-tags。
采纳答案by Kornel
Every browser that supports XHTML (Firefox, Opera, Safari, IE9) supports self-closing syntax on every element.
每个支持 XHTML 的浏览器(Firefox、Opera、Safari、IE9)都支持每个元素的自关闭语法。
<div/>
, <script/>
, <br></br>
all should work just fine. If they don't, then you have HTMLwith inappropriately added XHTML DOCTYPE.
<div/>
, <script/>
,<br></br>
一切正常。如果没有,那么您的HTML 中添加了不恰当的 XHTML DOCTYPE。
DOCTYPE does not change how document is interpreted. Only MIME type does.
DOCTYPE 不会改变文档的解释方式。只有 MIME 类型可以。
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.
HTML WG 已经讨论过这个问题:其目的是允许旧的(仅 HTML)浏览器通过遵循这些指南来接受 XHTML 1.0 文档,并将它们作为 text/html 提供。因此,作为 text/html 的文档应该被视为 HTML 而不是 XHTML。
It's a very common pitfall, because W3C Validator largely ignores that rule, but browsers follow it religiously. Read Understanding HTML, XML and XHTMLfrom WebKit blog:
这是一个非常常见的陷阱,因为 W3C 验证器在很大程度上忽略了该规则,但浏览器会虔诚地遵循它。从 WebKit 博客阅读 理解 HTML、XML 和 XHTML:
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 文档都是以
text/html
. 这意味着它们根本不是 XHTML,而是实际上通过 HTML 解析器错误处理而得到的无效 HTML。所有那些“有效的 XHTML 1.0!” 网络上的链接实际上是在说“无效的 HTML 4.01!”。
To test whether you have real XHTML or invalid HTML with XHTML's DOCTYPE, put this in your document:
要使用 XHTML 的 DOCTYPE 测试您是否有真正的 XHTML 或无效的 HTML,请将其放入您的文档中:
<span style="color:green"><span style="color:red"/>
If it's red, it's HTML. Green is XHTML.
</span>
It validates, and in real XHTML it works perfectly (see: 1vs 2). If you can't believe your eyes (or don't know how to set MIME types), open your page via XHTML proxy.
它验证,并且在真正的 XHTML 中它完美地工作(参见:1vs 2)。如果您不敢相信自己的眼睛(或不知道如何设置 MIME 类型),请通过XHTML 代理打开您的页面。
Another way to check is view source in Firefox. It will highlight slashes in red when they're invalid.
另一种检查方法是在 Firefox 中查看源代码。当它们无效时,它会以红色突出显示斜线。
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.
在 HTML5/XHTML5 中,这一点没有改变,而且区别更加清晰,因为您甚至没有额外的DOCTYPE
. Content-Type
是国王。
For the record, the XHTML spec allows any element to be self-closing by making XHTML an XML application: [emphasis mine]
作为记录,XHTML 规范允许任何元素通过使 XHTML 成为XML 应用程序来自我关闭:[强调我的]
Empty-element tags may be used for any element which has no content, whether or not it is declared using the keyword EMPTY.
空元素标签可用于任何没有内容的元素,无论是否使用关键字 EMPTY 声明。
It's also explicitly shown in the XHTML spec:
它也在XHTML 规范中明确显示:
Empty elements must eitherhave an end tag or the start tag must end with
/>
. For instance,<br/>
or<hr></hr>
空元素必须要么有结束标签或者开始标签必须结束
/>
。例如,<br/>
或<hr></hr>
回答by Erik van Brakel
One element to be very careful with on this topic is the <script
> element. If you have an external source file, it WILL cause problems when you self close it. Try it:
在这个主题上需要非常小心的一个元素是<script
> 元素。如果您有外部源文件,当您自行关闭它时会导致问题。尝试一下:
<!-- this will not consistently work in all browsers! -->
<script type="text/javascript" src="external.js" />
This will work in Firefox, but breaks in IE6 at least. I know, because I ran into this when over-zealously self closing every element I saw ;-)
这将在 Firefox 中工作,但至少在 IE6 中会中断。我知道,因为我在过度热情地自我关闭我看到的每个元素时遇到了这个问题;-)
回答by hsivonen
The self-closing syntax works on all elements in application/xhtml+xml. It isn't supported on any element in text/html, but the elements that are “empty” in HTML4 or “void” in HTML5 don't take an end tag anyway, so if you put a slash on those it appears as though the self-closing syntax were supported.
自闭合语法适用于 application/xhtml+xml 中的所有元素。text/html 中的任何元素都不支持它,但是 HTML4 中为“空”或 HTML5 中为“空”的元素无论如何都不会带结束标记,因此如果在这些元素上加上斜线,它看起来就像支持自关闭语法。
回答by ConroyP
From the W3 Schools reference site:
<area />
<base />
<basefont />
<br />
<hr />
<input />
<img />
<link />
<meta />
回答by Dmitry Osinovskiy
Better question would be: what tags can be self-closed even in HTML mode without affecting code? Answer: only those that have empty content (are void). According to HTML specsthe following elements are void:
更好的问题是:即使在 HTML 模式下,哪些标签也可以自关闭而不影响代码?答:只有那些内容为空的(无效)。根据HTML 规范,以下元素无效:
area, base, br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr
area, base, br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr
Older version of specification also listed command
.
Besides, according to various sources the following obsolete or non-standard tags are void:
还列出了旧版本的规范command
。此外,根据各种来源,以下过时或非标准标签无效:
basefont, bgsound, frame, isindex
basefont, bgsound, frame, isindex
回答by Jeff
Hope this helps someone:
希望这有助于某人:
<base />
<basefont />
<frame />
<link />
<meta />
<area />
<br />
<col />
<hr />
<img />
<input />
<param />
回答by AmbroseChapel
What about <meta>
and <link>
? Why aren't they on that list?
怎么样<meta>
和<link>
?为什么他们不在那个名单上?
Quick rule of thumb, do not self-close any element which is intended to have content, because it will definitely cause browser problems sooner or later.
快速的经验法则,不要自行关闭任何旨在包含内容的元素,因为它迟早肯定会导致浏览器问题。
The ones which are naturally self-closing, like <br>
and <img>
, should be obvious. The ones which aren't ... just don't self-close them!
那些自然自闭的,比如<br>
和<img>
,应该是显而易见的。那些不是......只是不要自我关闭它们!
回答by Shadow2531
The last time I checked, the following were the empty/void elements listed in HTML5.
我上次检查时,以下是 HTML5 中列出的空/空元素。
Valid for authors: area, base, br, col, command, embed, eventsource, hr, img, input, link, meta, param, source
对作者有效:area、base、br、col、command、embed、eventsource、hr、img、input、link、meta、param、source
Invalid for authors: basefont, bgsound, frame, spacer, wbr
对作者无效:basefont、bgsound、frame、spacer、wbr
Besides the few that are new in HTML5, that should give you an idea of ones that might be supported when serving XHTML as text/html. (Just test them by examining the DOM produced.)
除了 HTML5 中的一些新内容之外,这应该会让您了解在将 XHTML 作为文本/html 提供服务时可能支持的内容。(只需通过检查生成的 DOM 来测试它们。)
As for XHTML served as application/xhtml+xml (which makes it XML), XML rules apply and any element can be empty (even though the XHTML DTD can't express this).
至于作为 application/xhtml+xml 的 XHTML(这使它成为 XML),XML 规则适用并且任何元素都可以为空(即使 XHTML DTD 无法表达这一点)。
回答by mpen
They're called "void" elements in HTML 5. They're listed in the official W3 spec.
它们在 HTML 5 中称为“void”元素。它们列在官方 W3 规范中。
A void element is an element whose content model never allows it to have contents under any circumstances.
void 元素是其内容模型在任何情况下都不允许其具有内容的元素。
As of April 2013, they are:
截至 2013 年 4 月,它们是:
area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr
area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr
As of December 2018 (HTML 5.2), they are:
截至 2018 年 12 月 (HTML 5.2),它们是:
area, base, br, col, embed, hr, img, input, link, meta, param, source, track, wbr
area, base, br, col, embed, hr, img, input, link, meta, param, source, track, wbr
回答by e-satis
You should have a look the xHTML DTDs, they're all listed. Here is a quick review all the main ones:
你应该看看xHTML DTDs,它们都被列出来了。以下是所有主要内容的快速回顾:
<br />
<hr />
<img />
<input />