Html 可以使用自关闭 DIV 标签吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7971716/
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
Is it OK to use a self closing DIV tag?
提问by Abe Miessler
Possible Duplicate:
Are self-closing tags valid in HTML5?
可能的重复:
自闭合标签在 HTML5 中有效吗?
For example:
例如:
<div id="myDiv" />
Something would then be done to populate this div using Javascript.
然后会做一些事情来使用 Javascript 填充这个 div。
Is this valid HTML?
这是有效的 HTML 吗?
采纳答案by Mark Byers
No. HTML 4.x doesn't have any concept of self closing tags.
不。HTML 4.x 没有任何自结束标记的概念。
It is valid in XHTML.
它在 XHTML 中有效。
回答by scrappedcola
Div's are not valid self closing tags. To have an empty div it would be better to do this:
Div 不是有效的自结束标签。要拥有一个空的 div,最好这样做:
<div id="myDiv"></div>
回答by Kerrek SB
According to the XML declaration and the XHTML 1.0 and 1.1 document definitions, this is fine: the null-end tag (>
) may be used when immediately following the null-end start tag closer (/
), and your code is equivalent to <div id="myDiv"></div>
.
根据 XML 声明和 XHTML 1.0 和 1.1 文档定义,这很好:>
当紧跟在更接近的空结束开始标记 ( /
)之后时可以使用空结束标记 ( ),并且您的代码等效于<div id="myDiv"></div>
。
It's a different matter entirely whether any particular consumer will be able to process this correctly.
是否有任何特定的消费者能够正确地处理这完全是另一回事。
The SGML declaration used by HTML 4.01 allows tag shortening, but it has a different syntax for the null-end tags; there you can write <div id="abc"/this is a non-empty div/
. Again, mileage may vary as for browser support. (My money is on "none".)
HTML 4.01 使用的 SGML 声明允许缩短标签,但它对空端标签有不同的语法;在那里你可以写<div id="abc"/this is a non-empty div/
。同样,浏览器支持的里程可能会有所不同。(我的钱是“无”。)
Future versions of HTML (HTML5? if that name is still alive) are no longer implemented as SGML languages, and therefore they simply allow what they say they do, without recourse to a formal grammar.
HTML 的未来版本(HTML5?如果那个名字还活着的话)不再作为 SGML 语言实现,因此它们只是允许他们说他们所做的,而不求助于正式的语法。
回答by defau1t
Self Closing Tags in XHTML as implemented by browsers:
由浏览器实现的 XHTML 中的自关闭标签:
What are all the valid self-closing elements in XHTML (as implemented by the major browsers)?
XHTML 中所有有效的自关闭元素是什么(由主要浏览器实现)?
Self Closing tags in html5:
html5 中的自关闭标签:
回答by joshuariojas
I ran these two blocks of code through the W3C validator. Copy and paste the code into the input under the Validate by Direct Input tab to see the results for yourself.
我通过 W3C验证器运行了这两个代码块。将代码复制并粘贴到 Validate by Direct Input 选项卡下的输入中,以亲自查看结果。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>title</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" >
</head>
<body><div id="Mydiv" /></body>
</html>
The code block with Doctype of transitional HTML 4.01 failed the validation process.
Doctype 为过渡 HTML 4.01 的代码块未通过验证过程。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Test</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
</head>
<body><div id="Mydiv" /></body>
</html>
When I added the XHTML 1.0 transitional doctype, changed the meta tag to a self closing tag, and added in the html xmlns line, the validation passed.
当我添加 XHTML 1.0 过渡文档类型,将元标记更改为自结束标记,并在 html xmlns 行中添加时,验证通过。
So to answer the first half of your question, it is valid HTML under the XHTML 1.0 Transitional doctype. Whether or not you can use javascript to properly populate it, I am not sure.
因此,要回答您问题的前半部分,它是 XHTML 1.0 Transitional doctype 下的有效 HTML。您是否可以使用 javascript 正确填充它,我不确定。
回答by Anders
No, it's valid XML (not HTML), and as far as I know, will only be accepted if the document is send with an application/xml mimetype.
不,它是有效的 XML(不是 HTML),据我所知,只有在使用 application/xml mimetype 发送文档时才会被接受。
However, it may work with XHTML, and the XHTML Doctype declaration.
但是,它可以与 XHTML 和 XHTML Doctype 声明一起使用。