在我的网站上查看源代码总是看起来很糟糕,为什么 HTML 没有正确排列?
我在格式化 HTML 和保持简洁方面做得不错,但每次我查看源代码时,都会发现到处都是元素。我想这很好,因为它不会使页面加载更快或更慢,并且使复制变得更加困难,但它看起来很丑,我希望它没有
为什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在网络浏览器中查看源代码将准确显示服务器发送到客户端的内容。如果您确实很好地格式化了 HTML,但它在客户端上看起来并不完全相同,那么中间还有其他一些因素导致其排列不一样,例如 PHP 或 ASP 等服务器端技术。 NET,它被用来生成一些标记。
此外,由于空间的原因,您可能会看到不同的结果。例如,如果在您的开发环境中混合使用空格和制表符,并且一个制表符等于 4 个空格,然后在浏览器中可能是一个制表符等于 8 个空格,那么事情就不会正确排列。要解决此问题,请始终使用制表符或始终使用空格。大多数不错的 IDE 都会自动为您在制表符和空格之间切换(例如 Visual Studio)。
一些浏览器工具(例如 Firebug 和 Chrome 的开发人员工具)将按照浏览器理解的方式显示 DOM 树。这是 DOM 回 HTML 的转换,不太可能与服务器发送的内容完全相同。不过它的格式很完美。
View Source in a web browser will show exactly what the server sent to the client. If you're really formatting your HTML nicely and it doesn't look exactly the same on the client, then there's something else in the middle that's making it not line up the same, such as a server-side technology like PHP or ASP.NET which is being used to generate some of the markup.
Also it's possible you're seeing it different due to spaces. If in your development environment you're mixing spaces and tabs and have one tab equal to 4 spaces, for example, and then in the browser it might be one tab equal to 8 spaces, then things won't line up right. To fix this, either always use tabs or always use spaces. Most decent IDEs will swap between tabs and spaces automatically for you (like Visual Studio).
Some browser tools like Firebug and Chrome's Developer Tools will show the DOM tree as the browser understands it. This is a translation of the DOM back to HTML and is not likely to be the exact same as what the server sent the content. It is formatted perfectly though.
我不确定为什么您的 HTML 在浏览器的“查看源代码”中未正确排列。实际查看您的 HTML 会很有帮助。
一些常见的罪魁祸首包括:
,您可以查看 HTML 整洁。我还没用过,但一直听说不错。
I'm not sure why your HTML is not lined up properly in your browser's View Source. It would be helpful to actually see your HTML.
Some of the common culprits include:
It may be useful for you to look at HTML Tidy. I haven't used it yet, but I've always heard good things.