0

例如,人们为什么要写:

<html>
<body>
   <p>blahblahblah</p>
   <p>blahblah</p>
</body>
</html>

代替

<html>
<body>
<p>blahblahblah</p>
<p>blahblah</p>
</body>
</html>

我刚刚完成了一个 HTML 课程,但从来没有知道为什么会这样。只是为了让人们更容易阅读代码吗?

4

4 回答 4

0

Indentation has no purpose beyond readability of code, which is of course subjective. Readability in particular relates to visualizing the nesting of elements. This may help to avoid problems like accidental omission of end tags.

There are varying practices, regarding the amount of indentation and logic in using it (e.g., some people would also indent the body element, since it is a child of the html element).

Quite often, indentation is there just because some authoring software produces it automatically, and authors see no reason to override it.

Indentation is normally ignored by browsers, since any sequence of whitespace characters (spaces, tabs, line breaks) is equivalent to a single space in most contexts in HTML. Sometimes it is not, and then indentation causes problems.

于 2013-10-08T20:43:32.450 回答
0

最后,您可以随时使用 Dave Raggett 的“Tidy”为您清理页面。 http://www.w3.org/People/Raggett/tidy/

于 2013-10-08T22:24:25.803 回答
0

这是一个约定,因为它可以更轻松地可视化文档的结构,无需使用已关闭标签的解析工具即可定位,并有助于在视觉上分离页面组件。

总而言之,因为从长远来看更容易编写、阅读和维护,所以采用某种可视化组织策略。

于 2013-10-08T20:04:49.203 回答
0

这一切都是为了在您下次打开代码时能够理解您的代码。它有助于确保您的代码是免费的或错误的,并使调试变得容易。

于 2013-10-08T20:06:47.620 回答