回车会导致 Internet Explorer 8 中出现视觉空间
我的 html 代码中的回车会导致 Explorer 8 中渲染的 html 中出现视觉空间。我猜这也会影响其他版本。
例如:
<span>
(111)
222-
3333
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)%>)
<%=((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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://www.w3.org/TR/xhtml1/#C_15
另外http://www.w3.org/TR/xhtml1/#uaconf (第 9 点):
http://www.w3.org/TR/xhtml1/#C_15
Also http://www.w3.org/TR/xhtml1/#uaconf (point 9):
这是
sprintf
类型函数真正发挥作用的场景。在 .NET 世界中,这些由String.Format
处理。这是 MSDN 文档 和您可以像这样重写代码:我对 .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 byString.Format
. Here's the MSDN documentation and you could rewrite the code something like this: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/
不仅仅是在 IE8 中。我用 chrome 测试了它,它给出了相同的结果。首先,您需要删除 << 中的空格跨度> < /跨度>元素本身。事实上,(111) 222 和 3333 44444 之间有 2 个空格。因此,似乎每个回车符都占用一个空格。
考虑到这个 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...
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.
换行符是一个空格。
如果您确实希望将这些内容放在单独的行中,则可以使用 HTML 注释:
尽管这对于可读性来说也不太好。就我个人而言,我不认为将所有内容都放在一行中是不可读的。您可以这样做:
如果您觉得
使其不可读。
A newline is a space.
If you really want those things on separate lines, you could use HTML comments:
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:
if you feel the
s are making it unreadable.