2

我使用 JSON 对 ASP.net Web 服务使用 JQuery 进行以下 AJAX 拉取:

$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "TestWebService.asmx/Foo",
data: "{}",
dataType: "json",
success: function(msg) {
    $("#justpre").html(msg.d);
    $("#precode").html(msg.d);
} } );

TestWebService 实现了一个非常简单的 WebMethod Foo(),它返回以下内容:

[WebMethod]
public string Foo() {
    return "multi" + Environment.NewLine + "line" + Environment.NewLine + "comment";
}

最后,我显示结果

<pre id="justpre"></pre>

<pre><code id="precode"></code></pre>

Firefox 和 Chrome 将返回值显示为多行注释就好了。然而,IE7 将其呈现为没有换行符的单行。

FF, Chrome:
multi
line
comment

IE7:
multi line comment

我怎样才能解决这个问题?

4

2 回答 2

2

IE 在将文本插入前置标记时存在一些已知问题。

以下是更多信息:http ://www.quirksmode.org/bugreports/archives/2004/11/innerhtml_and_t.html

您最好的选择可能是嗅探 IE 并将 \n 字符替换为<br />元素或嗅探 IE 并使用 IE only innerText,而不是 innerHTML。就像是

document.createTextNode("multi\nline\ncomment");

也可能以跨浏览器的方式来解决问题。

于 2009-10-22T05:23:29.000 回答
0

不要使用“pre”标签,而是尝试在父标签上使用 CSS

.theParentTagOfPre {
   white-space: pre-wrap;
}

有关空白的更多信息

于 2009-10-22T07:57:03.803 回答