Html 我是否需要在 <img> 或 <br> 标签等末尾添加“/”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15149641/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:00:28  来源:igfitidea点击:

Do I need a "/" at the end of an <img> or <br> tag, etc.?

htmlimage

提问by MarJamRob

Do you need to have a /at the end of an imgtag? I saw an exampleon W3schools.com without a /:

你需要/img标签的末尾有一个吗?我在 W3schools.com 上看到了一个没有/:

<img src="smiley.gif" alt="Smiley face" height="42" width="42">

I know it isn't necessary to self-close the tag, at least in my browser, but should I do so?

我知道没有必要自行关闭标签,至少在我的浏览器中是这样,但我应该这样做吗?

回答by Danny Beckett

The /is only required for XHTML & XML.

/只需要XHTML和XML。

If you're using a HTML5 doctype, then there's no need to terminate self-closing tagsin this way.

如果您使用的是 HTML5 文档类型,则无需以这种方式终止自关闭标签

This applies to <img src="img.png" />, <br />, <hr />etc.

这适用于<img src="img.png" /><br /><hr />等。

I.e. Just use <img src="img.png">, <br>and <hr>.

即只需使用<img src="img.png">,<br><hr>

If you need an empty element (like a div), don't use <div />, instead use <div></div>. This is important since in HTML5, the slash is ignoredand <div />is interpreted as <div>without a closing tag.

如果您需要一个空元素(如 a div),请不要使用<div />,而是使用<div></div>. 这很重要,因为在 HTML5 中,斜杠被忽略并被<div />解释为<div>没有结束标记。

回答by Niet the Dark Absol

It's only required for XHTML standards, as mentioned in other answers. HOWEVER, it also has another use.

如其他答案中所述,仅 XHTML 标准需要它。然而,它还有另一个用途。

Some code editors such as Notepad++ allow you to expand/collapse tags to make for faster viewing. But if you just put <img>, how is it supposed to know the difference between a tag that doesn't require an end tag, and one that uses the same tag name but does (ie. in XML)? This is especially true if you use custom tags.

某些代码编辑器(例如 Notepad++)允许您展开/折叠标签以加快查看速度。但是如果你只是把<img>,它应该如何知道不需要结束标签的标签和使用相同标签名称但需要(即在XML中)的标签之间的区别?如果您使用自定义标签,则尤其如此。

Therefore, using />explicitly tells the editor that it's self-closing, allowing it to continue working just fine and not having it warn you about a mismatched tag.

因此, using/>明确告诉编辑器它是自动关闭的,允许它继续正常工作,并且不会警告您标签不匹配。

回答by Jukka K. Korpela

Apart from the validity issue, which simply depends on the doctype you are validating against, the slash really matters only if your page is served with an XML content type, such as application/xhtml+xml(which is rarely used, because old versions of IE choke on it).

除了有效性问题,这仅取决于您验证的文档类型,只有当您的页面使用 XML 内容类型提供时,斜杠才真正重要,例如application/xhtml+xml(很少使用,因为旧版本的 IE 会阻塞它) .

If you do that, then well-formedness error handling is Draconian: any error (such as a start tag without a matching end tag, which can be the start tag itself when the <img ... />syntax is used) prevents the page from being displayed at all, and instead an error message is shown.

如果你这样做,那么格式良好的错误处理是严厉的:任何错误(例如没有匹配的结束标签的开始标签,当使用<img ... />语法时它可以是开始标签本身)完全阻止页面显示,而是显示错误消息。

回答by mitim

I think it would be better to close the img tag. Off the top of my head I can think of 2 reasons:

我认为关闭 img 标签会更好。在我的脑海里,我能想到两个原因:

  1. It won't validate under xhtml, if you care about this sort of thing.

  2. Anything/anyone trying to programmatically work with it may get confused/run in to issues about unclosed tags. Who knows, this may include you in the future. :)

  1. 如果您关心这类事情,它不会在 xhtml 下验证。

  2. 任何试图以编程方式使用它的人/任何人都可能会混淆/遇到有关未关闭标签的问题。谁知道呢,这将来可能包括你。:)

回答by Snippet

The code is acceptable in html without using /but it is required in XHTML. I prefer to put /on so that there is no problem migrating from HTML to XHTML

该代码在 html 中可以不使用,/但在 XHTML 中是必需的。我更喜欢穿上 /这样从 HTML 迁移到 XHTML 没有问题