部分视图
<script type="text/javascript">
document.write("<script src='http://ad.reklamport.com/rpgetad.ashx?tt=t_iddaa_habersayfalari_mac_detay_300x250&ciid=&rnd=" + Math.random() % 99999999 + "'></" + "script>");
</script>
查看
有效..
<html>
<head>
</head>
<body>
@{Html.RenderAction("Partial");}
</body>
</html>
它不起作用......
<body>
@{Html.RenderAction("Partial");}
<script type="text/javascript">
$.ajax({
type: "post",
url: "/Home/Partial",
dataType: "html",
success: function (result) {
$("#content").html(result);
},
error: function (result) {
}
});
</script>
<div id="content">
</div>
</body>
我需要使用第二种方式..问题是什么?
答案 0 :(得分:0)
这是因为<div id="content">
尚未加载到dom。尝试将JS脚本放在内容div之后
<body>
@{Html.RenderAction("Partial");}
<div id="content">
</div>
<script type="text/javascript">
$.ajax({
type: "post",
url: "/Home/Partial",
dataType: "html",
success: function (result) {
$("#content").html(result);
},
error: function (result) {
}
});
</script>
</body>
或者您可以包装您的ajax请求文档就绪事件处理程序
<script type="text/javascript">
$(function{
$.ajax({
type: "post",
url: "/Home/Partial",
dataType: "html",
success: function (result) {
$("#content").html(result);
},
error: function (result) {
}
});
});
</script>