在带有空白的 PRE 中美化突出显示:nowrap 在 IE 中是一行

发布于 2024-08-16 22:51:57 字数 817 浏览 11 评论 0原文

在 Internet Explorer 中,样式为 white-space:nowrapPRE 会导致 PRE 中的代码在以下情况下位于一行中:我使用 prettify (http://code.google.com/p/google-code -美化/)。

难道
不应该在 PRE 中产生新行吗?我在页面加载后检查了 prettify 生成的 HTML 源,当遇到换行符时它会生成

请参阅 http://blog.mikecouturier.com /2009/12/google-street-view-with-google-maps_27.html 为例。

这在 FF 和 Chrome 中都不会发生。

谢谢

编辑:也许这与此有关:将换行符插入预标记(IE、Javascript)

In Internet Explorer, a PRE with a style of white-space:nowrap is causing the code in the PRE to be in one single line when I use prettify (http://code.google.com/p/google-code-prettify/).

Doesn't a <br/> is supposed to cause a new line in a PRE? I checked the HTML source generated by prettify after the page has loaded and it generates <br/> when it encounter newlines.

See http://blog.mikecouturier.com/2009/12/google-street-view-with-google-maps_27.html for an example.

This doesn't happen in FF nor Chrome.

Thanks

EDIT: Maybe this is related to this: Inserting a newline into a pre tag (IE, Javascript)

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

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

发布评论

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

评论(2

玩心态 2024-08-23 22:51:57

来自Internet Explorer innerHTML Quirk

HTML的innerHTML属性
元素是众所周知且广泛的
用过的。它能够设置
一个元素的完整内容
go,包括元素之类的。
作为 QuirksMode
测试

已经表明,innerHTML 是最快的
动态改变页面的方法
内容。

但是,innerHTML 有一个问题
Internet Explorer。

HTML 标准要求
内容显示的转换。
各种类型和数量的相邻
空格被折叠成一个
空间。这是一件好事——就像
一个例子,它让我可以添加很多东西
此源文件中的换行符
不用担心奇怪的事情
显示文本中的换行符。

Internet Explorer 应用这些
分配给的转换
内部 HTML 属性。这似乎是一个
好主意:节省一点时间
在显示期间,因为如果
内存中的表示已经是
标准化,那么浏览器不会
必须在需要时进行正常化
显示文本。

也有例外情况
不过,标准化规则。尤其,
这些是

这是另一个 IE 独有的“bug”(即使是他们发明了innerHTML)。在第一个链接中,你有这个问题的解决方案,但正如卢卡斯指出的那样,所有这些似乎都指向谷歌的代码,而你无法控制。

From The Internet Explorer innerHTML Quirk:

The innerHTML property of HTML
elements is well-known and widely
used. It is capable of setting the
complete content of an element in one
go, including elements and the like.
As the QuirksMode
tests

have shown, innerHTML is the fastest
way to dynamically change the page
content.

However, innerHTML has a problem in
Internet Explorer.

The HTML standard requires a
transformation on display of content.
All kinds and amounts of adjacent
whitespace are collapsed into a single
space. This is a good thing - just as
an example, it allows me to add a lot
of line breaks into this source file
without having to worry about weird
line breaks in the displayed text.

Internet Explorer applies these
transformations on assignment to the
innerHTML property. This seems like a
good idea: it saves a little time
during display, because if the
in-memory representation is already
normalized, then the browser doesn't
have to normalize whenever it needs to
display the text.

There are exceptions to the
normalization rule, though. Notably,
these are the <textarea> element,
the <pre> element and, in CSS-aware
browsers, elements with any value but
normal for the white-space property.

Internet Explorer does not respect
these special cases. The third makes
their optimization a bad idea, because
white-space might change at runtime,
for example through the DOM. In any
case, Internet Explorer will normalize
all assignments to the innerHTML
property.

This is another IE only "bug" (even if it's them who invented innerHTML). In the first link you have solutions to this problem but as Lucas pointed out all seems to point at code by Google which you have no control over.

余生一个溪 2024-08-23 22:51:57

嘿litb,你是对的,在IE6和IE7版本中,pre标签代码显示在1行上。我仅针对 IE6 和 7 浏览器添加了这个小修复。

我已经测试过它,它工作正常,这有点 hacky,因为我实际上用

标签替换
标签,但这并不'不会在 IE6 和 7 浏览器中造成任何视觉差异。

您可以将此代码片段添加到 HTML 页面的任何位置。它使用 jQuery。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
    // this BR issue seems to happen only in browser
    // msie version 6.0 and 7.0
    if($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0")) {

        // replace all <br>s in the .prettyprint section
        // with <p> tags. <p> tags have the same visual behavior as <br> tags
        // in IE 6 and 7
        $(".prettyprint br").each(function(){
            $(this).replaceWith($("<p></p>"));
        });

    }

});
</script>

在谷歌修复 IE6 和 IE6 的美化代码之前,这可能是您最好的选择。 7.

Hey litb, you're right, in versions IE6 and IE7 the pre tag code is displayed on 1 line. I've added this small fix for browsers IE6 and 7 only.

I've tested it and it works fine, it's a bit hacky because I'm actually replacing <br> tags with <p> tags, but this doesn't cause any visual difference in the IE6 and 7 browsers.

You can add this snippet of code anywhere on your HTML page. It's using jQuery.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
    // this BR issue seems to happen only in browser
    // msie version 6.0 and 7.0
    if($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0")) {

        // replace all <br>s in the .prettyprint section
        // with <p> tags. <p> tags have the same visual behavior as <br> tags
        // in IE 6 and 7
        $(".prettyprint br").each(function(){
            $(this).replaceWith($("<p></p>"));
        });

    }

});
</script>

This is probably your best option until google fixes their prettify code for IE6 & 7.

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