html - 什么时候需要向 HTML 元素添加尾部斜杠?

标签 html

考虑 this AngularJS tutorial 中的以下代码片段:

<div class="auth-forms">

  <h2>Log In</h2>

  <form ng-submit="login()">
    <input type="email" ng-model="user.email" placeholder="Email" class="form-control"><br>
    <input type="password" ng-model="user.password" placeholder="Password" class="form-control"><br>
    <input type="submit" value="Log in" class="btn btn-primary" />
  </form>

</div>

前两个<input>标签不以尾部斜杠结尾,但最后一个以斜杠结尾。我注意到他在教程的其他地方写道 <br/>而在这里他只是写<br> 。这两种符号有什么区别吗?他在不同位置以不同的方式书写它们是否有原因?

最佳答案

回答

尾部斜杠代表 XML兼容性,在XHTML时代

如果你有内容,你可以写:

<a href="http://domain.com">Content (Description of the Hyperlink)</a>

如果您没有编写内容:

<img src="image.jpg" />

它(在 XML 中)与:

<img src="image.jpg"></img>

<img>无效 XML ,但是<img /><img></img>有效。

你的例子

您提供的示例是 HTML 的混合和XHTML文档类型。这不是好的风格。要么你写HTML或者你写XHTML 。无论如何,普通浏览器都没有问题。

关于html - 什么时候需要向 HTML 元素添加尾部斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23816850/

相关文章:

javascript - 将值和 id 动态添加到隐藏的输入类型

html - 图像 slider 在 Chrome 中工作正常但在 IE11 中不工作

html - 从 css 添加很棒的字体

jquery - 如果已经在 HTML 中设置了宽度和高度,您能否使用 CSS(或 JS/JQ)按比例调整图像大小

php - 超链接到 php 页面正在下载页面

javascript - 提交前验证 jsp 中的表单

html - 使用 Less 动态增加 z-index

html - CSSOM 和 DOM 创建是异步的吗?

javascript - 下拉菜单中的下拉菜单不起作用

javascript - 从表 jQuery 中删除空行