問題描述
可能重復(fù):
在普通標簽上使用 XHTML 右斜杠 (/)?
自閉標簽在 HTML5 中有效嗎?
我正在審查大量 HTML 代碼以及合成 HTML 的 JavaScript,我注意到如果標簽內(nèi)有一些沒有內(nèi)容的標簽,那么有兩種方法可以指定它.像這樣:
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" />
這兩者有區(qū)別嗎?
推薦答案
瀏覽器將無效標記標準化為有效形式:
Browsers normalize invalid markup into the valid form:
<div/>
在技術(shù)上是無效標記 (HTML 5),因為 div
不是自閉合標簽.
<div />
is technically invalid markup (HTML 5), as div
is not a self-closing tag.
瀏覽器會將其規(guī)范化為 <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.
自閉合標簽沒有子標簽,也沒有內(nèi)部文本的值(null):
A self-closing tag has no children and no value for inner text (null):
<foo />
封閉標簽沒有子標簽,也沒有內(nèi)部文本(空字符串):
A closed tag has no children and no inner text (empty string):
<foo></foo>
這篇關(guān)于<tag></tag> 有什么區(qū)別?和<標簽/>在 HTML 中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!