回车会导致 Internet Explorer 8 中出现视觉空间

发布于 2024-08-16 15:52:17 字数 1052 浏览 5 评论 0原文

我的 html 代码中的回车会导致 Explorer 8 中渲染的 html 中出现视觉空间。我猜这也会影响其他版本。

例如:

<span>
(111)&nbsp;
222-
3333&nbsp;
444444
</span>

看起来像这样:

(111)  222- 3333  444444

第一个括号后面应该只有 1 个空格,破折号后面没有空格,最后 3 个后面只有 1 个空格。我喜欢回车符以提高代码可读性,是否可以继续保留然后仍然得到html 在 IE 中正确渲染?

这是文档类型信息:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

编辑:
我实际上是在 ASP.NET MVC 2 应用程序中执行此操作,并且 aspx 模板标记非常冗长,这就是为什么我尝试将其分成多行:

<span>
(<%=((Model == null || Model.AreaCode == null) ? "" : Model.AreaCode).PadRight(3)%>)&nbsp;
<%=((Model == null || Model.Prefix == null) ? "" : Model.Prefix).PadRight(3)%>-
<%=((Model == null || Model.Suffix == null) ? "" : Model.Suffix).PadRight(4)%>&nbsp;
<%=(Model == null || Model.Extension == null) ? "" : Model.Extension%>
</span>

A carriage return in my html code causes a visual space in the rendered html in Explorer 8. I'm guessing this will affect other versions too.

For example:

<span>
(111) 
222-
3333 
444444
</span>

looks like this:

(111)  222- 3333  444444

There should be only 1 space after the first bracket, no space after the dash and only 1 space after the last 3. I like the carriage returns for code readability, is it possible go keep then and still get the html to render properly in IE?

Here's the doctype information:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

Edit:
I'm actually doing this in an ASP.NET MVC 2 app, and the aspx template markup is quite verbose which is why I've tried to seperate it into multiple lines:

<span>
(<%=((Model == null || Model.AreaCode == null) ? "" : Model.AreaCode).PadRight(3)%>) 
<%=((Model == null || Model.Prefix == null) ? "" : Model.Prefix).PadRight(3)%>-
<%=((Model == null || Model.Suffix == null) ? "" : Model.Suffix).PadRight(4)%> 
<%=(Model == null || Model.Extension == null) ? "" : Model.Extension%>
</span>

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

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

发布评论

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

评论(4

颜漓半夏 2024-08-23 15:52:17

在 HTML 中,换页符
(U+000C) 被视为空白,在
XHTML

http://www.w3.org/TR/xhtml1/#C_15

另外http://www.w3.org/TR/xhtml1/#uaconf (第 9 点):

空白的处理根据
以下规则。下列
字符在 [XML] 中定义 白色
空格字符:

空格 ( ) 水平制表
( ) 回车符 ( )
换行(
) XML 处理器
标准化不同系统的线端
将代码放入一个换行符中
字符,即传递给
应用程序。

用户代理必须使用该定义
来自 CSS 来处理空白
字符[CSS2]。请注意,CSS2
建议没有明确
解决空白问题
处理非拉丁字符集。
这将在未来解决
CSS 版本,此时
参考将更新。

in HTML, the Formfeed character
(U+000C) is treated as white space, in
XHTML

http://www.w3.org/TR/xhtml1/#C_15

Also http://www.w3.org/TR/xhtml1/#uaconf (point 9):

White space is handled according to
the following rules. The following
characters are defined in [XML] white
space characters:

SPACE ( ) HORIZONTAL TABULATION
( ) CARRIAGE RETURN ( )
LINE FEED ( ) The XML processor
normalizes different systems' line end
codes into one single LINE FEED
character, that is passed up to the
application.

The user agent must use the definition
from CSS for processing whitespace
characters [CSS2]. Note that the CSS2
recommendation does not explicitly
address the issue of whitespace
handling in non-Latin character sets.
This will be addressed in a future
version of CSS, at which time this
reference will be updated.

在梵高的星空下 2024-08-23 15:52:17

这是 sprintf 类型函数真正发挥作用的场景。在 .NET 世界中,这些由 String.Format 处理。这是 MSDN 文档 和您可以像这样重写代码:

<span>
<%= String.Format("({0:###}) {1:###}-{2:####} {3}", Model.AreaCode, Model.Prefix, Model.Suffix, Model.Extension); %>
</span>

我对 .NET 字符串格式语法有点生疏,因此无法保证该代码片段。这是另一个链接:

http://blog.stevex.net/string-formatting-in -csharp/

This is a scenario where sprintf type functions really shine. In the .NET world these are handled by String.Format. Here's the MSDN documentation and you could rewrite the code something like this:

<span>
<%= String.Format("({0:###}) {1:###}-{2:####} {3}", Model.AreaCode, Model.Prefix, Model.Suffix, Model.Extension); %>
</span>

I'm a little rusty on the .NET string format syntax, so no guarantees on that code snippet. Here's another link:

http://blog.stevex.net/string-formatting-in-csharp/

情痴 2024-08-23 15:52:17

不仅仅是在 IE8 中。我用 chrome 测试了它,它给出了相同的结果。首先,您需要删除 << 中的空格跨度> < /跨度>元素本身。事实上,(111) 222 和 3333 44444 之间有 2 个空格。因此,似乎每个回车符都占用一个空格。

考虑到这个 HTML,这是完全可以的...

<html>
  <head>
  </head>
  <body>
    This is some static text
    with carriage return
    <span>
      (111) 222- 3333 444444
    </span>
  </body>
</html>

输出将是...

这是一些带有回车符的静态文本 (111) 222- 3333 444444

注意静态文本中文本和 with 之间的空格。因此,将回车符转换为空格似乎是 HTML 的一般规则。

Not just in IE8. I've tested it with chrome and it gives the same result. First you need to remove the space from < span> < /span> element it self. and infact there are 2 spaces between (111) 222 and 3333 44444. So it seems that each carriage return is taking one blank space.

This is perfectly ok consider this HTML...

<html>
  <head>
  </head>
  <body>
    This is some static text
    with carriage return
    <span>
      (111) 222- 3333 444444
    </span>
  </body>
</html>

the output would be...

This is some static text with carriage return (111) 222- 3333 444444

Watch for the space between text and with in static text. So translating carriage return to space is seems like a general rule for HTML.

清秋悲枫 2024-08-23 15:52:17

换行符是一个空格。

如果您确实希望将这些内容放在单独的行中,则可以使用 HTML 注释:

<span><!--
-->(111) <!--
-->222-<!--
-->3333 <!--
-->444444<!--
--></span>

尽管这对于可读性来说也不太好。就我个人而言,我不认为将所有内容都放在一行中是不可读的。您可以这样做:

<span style="white-space:nowrap">(111) 222-3333 444444</span>

如果您觉得   使其不可读。

A newline is a space.

If you really want those things on separate lines, you could use HTML comments:

<span><!--
-->(111) <!--
-->222-<!--
-->3333 <!--
-->444444<!--
--></span>

although that's not too great for readability either. Personally I don't see what's unreadable putting it all on one line. You could do:

<span style="white-space:nowrap">(111) 222-3333 444444</span>

if you feel the  s are making it unreadable.

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