可能重复:在普通标签上使用 XHTML 右斜杠 (/)?自闭标签在 HTML5 中有效吗?
我正在审查大量 HTML 代码以及合成 HTML 的 JavaScript,我注意到如果标签内有一些没有内容的标签,那么有两种方法可以指定它.像这样:
I'm reviewing a lot of HTML code and also JavaScript that synthesizes HTML and I noted that if there's some tag without content inside the tag then there're two way to specify it. Either like this:
<div id="container"></div>或者像这样:
<div id="container" />这两者有区别吗?
推荐答案浏览器将无效标记标准化为有效形式:
Browsers normalize invalid markup into the valid form:
<div/> 在技术上是无效标记 (HTML 5),因为 div 不是自闭合标签.
<div /> is technically invalid markup (HTML 5), as div is not a self-closing tag.
浏览器会将其规范化为 <div>.
A browser will normalize it to <div>.
请注意,这与 XML 处理自闭合标签和闭合标签的方式不同.
Note that this is different from how XML will handle a self-closing tag compared to a closed tag.
自闭合标签没有子标签,也没有内部文本的值(null):
A self-closing tag has no children and no value for inner text (null):
<foo />封闭标签没有子标签,也没有内部文本(空字符串):
A closed tag has no children and no inner text (empty string):
<foo></foo>更多推荐
<tag></tag> 有什么区别?和<标签/>在 HTML 中?
发布评论