如何防止HTML源格式影响输出?

栏目: Html · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/1737074/how-do-i-prevent-html-source-formatting-from-affecting-output
看起来 HTML

代码中的额外换行符可能会在最终输出中添加不需要的空格.我一直认为无论我如何布局我的HTML代码,它都不会影响渲染结果的样子.但这是一个例子:

<h2>
    <a href="#">Hello.</a>World
</h2>

将显示:“Hello.World” – 所有看起来都很好

<h2>
    <a href="#">Hello.</a>
    World
</h2>

将显示:“Hello.World” – 点后面有一个额外的空格!

有没有机会摆脱这种影响?

我希望将代码放在不同的行上 – 而不是产生额外的空间.

不,没有,不是在这种情况下.

在HTML中,所有空格都计为空格,但是几个空白字符之后只计为一个.所以,你的代码相当于:

<h2> <a href="#">Hello.</a> World </h2>

删除块元素旁边的空格,但不删除文本内的空格.由于锚标记是内联元素,因此无法删除其旁边的空间,因为这会更改内容.因此,在删除空间之后,这就是剩下的:

<h2><a href="#">Hello.</a> World</h2>

因此,只要不是内容的一部分,您可以在任何地方拥有额外的空白区域.这个:

<h2    >

  <p >  test      test    </p     >

  <p   >  test       test  </p  >

</h2   >

将等效于(删除不影响结果的空格后):

<h2><p>test test</p><p>test test</p></h2>

翻译自:https://stackoverflow.com/questions/1737074/how-do-i-prevent-html-source-formatting-from-affecting-output


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Letting Go of the Words

Letting Go of the Words

Janice (Ginny) Redish / Morgan Kaufmann / 2007-06-11 / USD 49.95

"Redish has done her homework and created a thorough overview of the issues in writing for the Web. Ironically, I must recommend that you read her every word so that you can find out why your customer......一起来看看 《Letting Go of the Words》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器