考虑 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/