我从$ .ajax从服务器获取内容。而这些内容的HTML字符串,他们有自己的风格欧文,以下是我的代码:
<script src="jquery-1.7.1.js" type="text/javascript"></script>
<link href="Default.css" rel="stylesheet" />
<link href="all.css" rel="stylesheet" />
<script type="text/javascript">
var dataStr = "indexOfStart=" + 0;
$(document).ready(function() {
$("#idgetData").click(function() {
$.ajax({
type: "post",
url: "http://localhost:1897/Content/Services/Service1.asmx/GetArticlesDuring",
dataType: "jsonp",
data: { indexOfStart: 0, indexOfEnd: 20 },
jsonp: 'back',
success: function (result) {
var res = decodeURIComponent(result.Result);
document.getElementById("content").innerHTML = res;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
targetDiv = $("#data");
if (errorThrown || textStatus == "error" || textStatus == "parsererror" || textStatus == "notmodified") {
targetDiv.replaceWith("请求数据时发生错误!");
return;
}
if (textStatus == "timeout") {
targetDiv.replaceWith("请求数据超时!");
return;
}
},
complete: function() {
//addStyle("Default.css");
//addStyle("all.css");
document.write('<link href="Default.css" rel="stylesheet" /><link href="all.css" rel="stylesheet" />');
}
});
});
});
function addStyle(stylePath) {
var container = document.getElementsByTagName("head")[0];
var addStyle = document.createElement("link");
addStyle.rel = "stylesheet";
addStyle.type = "text/css";
addStyle.href = stylePath;
container.appendChild(addStyle);
}
问题是内容的风格不能被加载和工作, 谁能帮助我吗?
是的,你是对的。我发现问题出在html中的内容字符串中,在样式类名称之前,有“+”(加号)。这是因为在发送内容的C#代码中,我使用了“Server.UrlEncode(string) “,这会造成”+“问题。我找到解决方案:在C#代码中,我使用“Microsoft.JScript.GlobalObject.encodeURIComponent(string)”来编码html内容,在客户端js代码中,我使用“decodeURIComponent”来解码。如此完美的方式,现在可以。并且非常感谢你! – francis
我很高兴我能够帮助,这是没有问题的 – BrotherBallan