我需要“/”吗?在< img>的结尾处或< br>标签等? [英] Do I need a "/" at the end of an <img> or <br> tag, etc.?

查看:94
本文介绍了我需要“/”吗?在< img>的结尾处或< br>标签等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否需要在 img 标签末尾加入 / ?我在W3schools.com上看到了示例,没有 / :

 < img src =smiley.gifalt =笑脸height =42 width =42> 

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

解决方案

/ XHTML&如果您使用HTML5文档类型,那么不需要以这种方式终止自闭合标签



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

即只要使用< img src =img.png> < br> 和<$ c $如果你需要一个空元素(比如 div ),不要使用< div /> ,而应使用< div>< DIV> 。这很重要,因为在HTML5中,斜杠被忽略< div /> 被解释为< div>

Do you need to have a / at the end of an img tag? I saw an example on W3schools.com without a /:

<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?

解决方案

The / is only required for XHTML & XML.

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

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

I.e. Just use <img src="img.png">, <br> and <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 ignored and <div /> is interpreted as <div> without a closing tag.

这篇关于我需要“/”吗?在&lt; img&gt;的结尾处或&lt; br&gt;标签等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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