我是 ASP.NET 的新手,但擅长 Java,我需要知道如何用新的 div 更新 div(div 元素可以在其中包含一些文本,想要更改)。我正在自己学习 ASP.NET,所以到目前为止我已经尽力做到了!让我们假设我的 Default.aspx 页面有一个 div 作为:
<head runat="server">
<title>Untitled Page</title>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
function change(){
$.ajax({
type: "GET",
url: "temp.aspx/Hello",
dataType: "html",
success: function(msg) {
console.log(msg);
$("#changer").html(msg);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="changer">
<asp:Label ID="country" runat="server">USA</asp:Label>
</div>
<input type="button"id="but" value="Hello Changer" onclick="change()"/>
</form>
</body>
我想用 id=changer 改变 div!让我们假设我的 temp.aspx 页面如下:
<body>
<form id="form1" runat="server">
<div>
Hello Text <asp:Label ID="lab" runat="server"></asp:Label>
</div>
</form>
</body>
所以 temp.aspx.cs 将是:
[WebMethod]
public static String Hello()
{
try
{
//Do here server event
System.Diagnostics.Debug.WriteLine("SomeText ");
//this method is not called by the AJAX for some of my blunder, I don't even know
//because if its called by the AJAX code the out will show SomeText string in output console!
return " Hello";
}
catch (Exception)
{
throw;
}
}
所以问题是我如何从 AJAX 代码调用 Web 方法,因为我正在自己学习 ASP.NET 有人可以帮助我吗 提前致谢!
最佳答案
我不太了解 ASP.NET,但是使用 AJAX 和 jQuery 你可以做到这一点:
$("#changer").load("new_div.html");
这会将文件 new_div.html
加载到 #changer
div
如果 new_div.html
文件中有一个特定的 div,您可以通过将它的 id 添加到 jQuery AJAX 调用来加载这个 div:
$("#changer").load("new_div.html#content");
这将加载仅
<div id="content">
Content...
</div>
来自 new_div
文件
关于javascript - 在 ASP.NET 中使用 AJAX 将现有的 div 替换为另一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32294399/