我应该如何格式化我的标记?
当谈到我的标记时,我是肛门的。它必须始终完美缩进、易于阅读,并且 100% 符合 W3C 标准。很多时候,当查看其他网站的标记时,我对开发人员缺乏努力来尝试保持浏览器中的标记干净、有组织和有效感到震惊。
另一方面,为了节省尺寸,很多人会将所有标记强制放在一条连续的线上。这也让我恼火,尽管程度不一样,因为这是有目的的。但在大多数情况下,似乎没有开发人员真正在浏览器中查看他们的标记并对此采取任何措施。
了解了这一点,对于浏览器中的解析器来说,缩进和空格(通常)并不重要,我应该如何处理我的标记?是否值得花费额外的时间来让我的标记对于人类和浏览器来说都非常容易阅读?我的\t
和\n
都白用了吗?
When it comes to my markup, I'm anal. It always has to be perfectly indented, easily readable to me, and 100% valid with the W3C. Often time, when viewing the markup of other websites, I'm appalled with the lack of effort by the developer to try to and keep their markup in the browser clean, organized, and valid.
On the flip side, there's a lot of people who will force all their markup on to one, continuous line for the size saving benefits. This annoys me as well, though not to the same extent because it is done with a purpose. But for the most part, it seems like no developer ever actually looks at their markup in the browser and does anything about it.
Understanding that, to the parser in the browser, indents and spaces (usually) don't matter, how should I be handling my markup? Is it worth the extra time to get my markup perfectly easily readable to humans as well as the browser? Are all my \t
's and \n
's being used in vain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有些浏览器存在错误,导致缩进的格式良好的 html 完全错误。例如某些版本的 Internet Explorer 带有表格和图像。
除此之外,我尝试保持合理的缩进,我不会花太多时间在它上面,只要足以使其易于调试即可。
There are some browsers who has bugs that renders indented well formed html completely wrong. Such as some versions of Internet explorer with tables and images.
Other than that, i try to keep sane indention, I don't spend to much time with it, just enough to make it easy to debug.
我的答案是不。论据:
My answer is no. The arguments:
恕我直言,这是值得付出努力的,因为它可以帮助您了解 html 页面中到底发生了什么,这绝对值得做一些事情。
如果我们想要编写干净、优雅的代码,这意味着我们也应该想要生成漂亮、干净、优雅的 html,不是吗?
It's worth the effort imho since it helps you understand what exactly is going on in your html page, and that's definitely worth something.
If we want to write clean, elegant code in general this means we should want to generate nice, clean elegant html as well, not?
不确定这是否回答了您的问题,但只要代码通过 W3C 有效,就可以按预期构建。至于您对代码(如查看源代码)结构的查看能力,这实际上取决于您,但我不会添加太多混乱(注释等)。为您的标记使用正确的 DOCTYPE,您应该可以接受。我认为没有任何理由“浪费”时间来使浏览器“书”中的源代码可读。查看源代码只会对您有利,因此您可以通过源代码视图快速了解正在发生的情况。
Not sure if this answers your question, but as long as the code is valid by W3C, is structured as intended. As far as your view-ability of the code (like view source) structure, that's really up to you, but I would not add too much clutter (comments etc). Use the correct DOCTYPE for your markup and you should be fine with that. I don't see any reason to "waste" time on making the source code from the browser "book" readable. The view source would only be beneficial to you so you can quickly see what's happening at a glance through source view.
我喜欢正确设置标记格式,并且我认为这样做可以更轻松地进行管理。
话又说回来,我使用 ASP.NET,大量标记是通过各种控件和类生成的。在这种情况下,我认为不值得尝试追踪每个未对齐的标记并查看是否可以采取某些措施来使关联的控件产生正确的结果。
简而言之,如果不需要付出巨大的努力就能完成,那么格式良好的标记是值得的。
I like to correctly format my markup, and I think it makes it easier to manage when I do.
Then again, I use ASP.NET and a lot of markup is generated through various controls and classes. In this case, I've decided it is not worth trying to track down each mis-aligned markup and see if something can be done to get the associated control to produce the correct result.
In short, nicely formatted markup is worth it if it can be accomplished without a huge effort.
是的,在我看来这是值得的。无论是现在还是将来,对于您和其他同事来说,维护都会更容易。
关于性能较低的缺点,为什么不开发一个缩进和注释良好的源文件并生成一个最小化版本在服务器上运行?它可以通过一系列简单的正则表达式替换来实现。
Yes, in my opinion it is worth. It will be easier to maintain, for you and for other collegues, now and in the future.
About the disadvantage of lower performance, why not to develop a well indented and commented source file and to generate a minimized version to run on the server? It can be acheived with a simple series of regex replacements.