我正在研究 jQuery AJAX,并且偶然发现了一个奇怪的现象(或者更确切地说,我缺乏理解:)。这是我的 ASPX 主页面的代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="jQuery_Test.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#btnGo').click(function() {
$.ajax({
type: "GET",
url: 'GetValues.aspx',
contentType: 'text/html',
success: function (html) { $("#Yo").append(html); }
});
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p id="Yo">Yo there!</p>
<br />
<a href="#" class="yo">Yo there!</a>
<br />
<a href="#">I'm in a DIV!</a>
<br />
<button id="btnGo">Go</button>
</div>
<br />
<br />
<a href="#">I'm not in a DIV!</a>
</form>
</body>
</html>
这是“GetValues.aspx”页面的隐藏代码:
使用系统;
namespace jQuery_Test
{
public partial class GetValues : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/html";
Response.Write("<a href='#'>Hello World</a>");
Response.End();
}
}
}
我所做的就是,当单击主页上的“Go”按钮时,我希望将 anchor 标记放置在段落旁边。
该代码确实有效,但是, anchor “Hello World”出现约 1 秒,然后消失。有人可以指出我做错了什么吗?我使用的是 VS 2010 Ultimate 附带的 jQuery-1.3.2.mins.js。
干杯。 贾斯。
最佳答案
使你的函数返回 false;
$(document).ready(function () {
$('#btnGo').click(function() {
$.ajax({
type: "GET",
url: 'GetValues.aspx',
contentType: 'text/html',
success: function (html) { $("#Yo").append(html); }
});
return false;
})
});
关于jQuery AJAX - 返回的 HTML 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2358707/