2

我正在使用 AJAX 和 jQuery 读取一个大文本文件:

$.ajax({
    type: "GET",
    url: "testing.txt",
    dataType: "text",
    success: function (returnText) {
        $("#viewDescription").text(returnText);
    }
});

然后,我想在请求完成时显示一个带有动画 GIF的弹出窗口。我使用以下代码做到了这一点:

$("#popup").on("ajaxSend", function () {
    $(this).show();
}).on("ajaxStop", function () {
    $(this).hide();
}).on("ajaxError", function () {
    $(this).hide();
});

<div style="display: none;" id="popup">
    <img src="loading.gif" />
</div>

弹出窗口正确显示和隐藏,但问题是 jQuery AJAX 调用运行时 GIF 停止。它不适用于任何浏览器。请问可能是什么问题?

4

2 回答 2

0

尝试这样的事情:

$('#mybtn').on('click', function(e) {
    e.preventDefault();
    $("#popup").show(); // Show the loader
    $.ajax({
        type: "GET",
        url: "testing.txt",
        dataType: "text",
        success: function(returnText) {
            $("#popup").hide(); // Hide on success
            $("#viewDescription").text(returnText);
        },
        error: function() {
            $("#popup").hide(); // Hide on error
        }
    });
});
于 2012-07-11T13:06:24.890 回答
0
Try this:
$('#mybtn').on('click', function(e) {
    $.ajax({
        type: "GET",
        url: "testing.txt",
        dataType: "text",
        beforeSend:function(){
             $("#popup").show(); // Show the loader
        },
        success: function(returnText) {
            $("#popup").hide(); // Hide on success
            $("#viewDescription").text(returnText);
        },
        error: function() {
            $("#popup").hide(); // Hide on error
        }
    });
});
于 2012-07-11T13:40:47.893 回答