在我的网站上查看源代码总是看起来很糟糕,为什么 HTML 没有正确排列?

发布于 2024-11-28 10:09:38 字数 113 浏览 0 评论 0 原文

我在格式化 HTML 和保持简洁方面做得不错,但每次我查看源代码时,都会发现到处都是元素。我想这很好,因为它不会使页面加载更快或更慢,并且使复制变得更加困难,但它看起来很丑,我希望它没有

为什么?

I do a decent job of formatting my HTML and keeping it clean, but every time I view source there are elements all over the place. I guess that's fine since it won't make the page load any faster or slower and makes it harder to copy, but it just looks ugly and I wish it didnt

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

濫情▎り 2024-12-05 10:09:38

在网络浏览器中查看源代码将准确显示服务器发送到客户端的内容。如果您确实很好地格式化了 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.

虐人心 2024-12-05 10:09:38

我不确定为什么您的 HTML 在浏览器的“查看源代码”中未正确排列。实际查看您的 HTML 会很有帮助。

一些常见的罪魁祸首包括:

  • 用于缩进代码的制表符和手动空格的混合(如果您想让事情看起来漂亮,请选择其中之一)。
  • 可能是 Windows、Unix 和 Macintosh 换行符 (CR/LF) 的混合,如果从多台计算机编辑代码,则可能会发生这种情况。我对此有疑问,但我不确定这是否会导致您所描述的问题;也许不是。 (我相信其他人可以对这种可能性进行更专业的评论。)
  • 您的网站是通过 CMS 进行管理的,该 CMS 会发出糟糕的 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:

  • a mixture of tabs and manual spaces for indenting code (if you want things to look pretty, do one or the other).
  • possibly a mixture of Windows, Unix, and Macintosh line breaks (CR/LF), which can happen if code is edited from multiple computers. I've had issues with this, but I'm not sure if it would cause the issues you're describing; perhaps not. (I'm sure others can comment more knowledgeably on that possibility.)
  • your site is managed through a CMS that emits terrible HTML

It may be useful for you to look at HTML Tidy. I haven't used it yet, but I've always heard good things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文