我通过 $.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);
}
问题是内容的样式无法加载和工作,有人可以帮我吗?