jQuery AJAX - 返回的 HTML 消失

标签 jquery ajax

我正在研究 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/

相关文章:

javascript - 改进表格渲染,最快的表格渲染

javascript - 简化 jQuery 事件

javascript - Node JS Mongoose 在 for 或 foreach 循环中更新查询,这可能吗?

javascript - Ajax 响应 JavaScript 变量

javascript - jQuery 处理复选框组

javascript - Promise 和延迟对象

javascript - 在不回发的情况下向 URL 添加/删除参数(查询字符串的功能)

javascript - 如何使用 ajax 加载更多 Logo ?

php - 显示数据库中的记录,然后在添加新记录时自动用新记录更新数据库中显示的记录

javascript - 如果通过 ajax 加载,则无法初始化 jssor slider