我通过使用 JSON 的 JQuery 将以下 AJAX 拉取到 ASP.net Web 服务:
$.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
我该如何解决这个问题?
最佳答案
IE 在将文本插入到 pre 标记时处理文本时存在一些已知问题。
这里有更多信息:http://www.quirksmode.org/bugreports/archives/2004/11/innerhtml_and_t.html
您最好的选择可能是嗅探 IE 并将\n 字符替换为 <br />
元素或嗅探 IE 并仅使用 IE innerText,而不是 innerHTML。有点像
document.createTextNode("multi\nline\ncomment");
也可能以跨浏览器的方式实现这一目的。
关于asp.net - 如何使 <pre><code></code></pre> 的内容在 IE7 中正确呈现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1605102/