<tag></tag> 有什么区别?和<标签/>在 HTML 中? [英] What's the difference between <tag></tag> and <tag /> in HTML?

查看:30
本文介绍了<tag></tag> 有什么区别?和<标签/>在 HTML 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在普通标签上使用 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>

这篇关于&lt;tag&gt;&lt;/tag&gt; 有什么区别?和&lt;标签/&gt;在 HTML 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆