简体   繁体   English

在Ajax期间显示/隐藏Div .load()

[英]Show/Hide Div During Ajax .load()

I would like to show the "LoaderMain" div before the .load() and when it is all complete hide the "LoaderMain". 我想在.load()之前显示“ LoaderMain” div,并在全部完成后隐藏“ LoaderMain”。 When I uncomment my show/hide it never displays. 当我取消评论/隐藏时,它永远不会显示。

$('#content').html('');
            //$('#LoaderMain').show();
            $("#content").load(url, function(response, status, xhr) {
            if (status == "error") {
                var msg = "Sorry but there was an error: ";
                $("#content").html(msg + xhr.status + " " + xhr.statusText);
            }

            });
            //$('#LoaderMain').hide();

Put $('#LoaderMain').hide(); $('#LoaderMain').hide(); in your callback function. 在您的回调函数中。

For example: 例如:

$('#content').html('');
$('#LoaderMain').show();
$("#content").load(url, function(response, status, xhr) {
    if (status == "error") {
        var msg = "Sorry but there was an error: ";
        $("#content").html(msg + xhr.status + " " + xhr.statusText);
    }
    $('#LoaderMain').hide();
});

Since load is asynchronous, you'll need your hide() function inside the callback: 由于加载是异步的,因此您需要在回调内部使用hide()函数:

$('#content').html('');
$('#LoaderMain').show();
$("#content").load(url, function(response, status, xhr) {
    if (status == "error") {
        var msg = "Sorry but there was an error: ";
        $("#content").html(msg + xhr.status + " " + xhr.statusText);
    }
    $('#LoaderMain').hide();
});
$("#content").load(url, function(response, status, xhr) {
   // Stuff 1
   $('#LoaderMain').hide();
});
// Stuff 2

Load is asynchronous , it means that the function Load will start, and while it runs, the script continues to stuff 2, and once the function running in background finshes, it does stuff 1. Note that if the function is very fast, stuff 1 can be done before stuff 2. Load是asynchronous ,这意味着Load将启动,并且在运行时,脚本将继续填充2,一旦该函数在后台运行,它将填充1。请注意,如果该函数非常快,则填充1可以先于东西2。

If the function was synchronous , stuff 1 would always be done before stuff 2. 如果该函数是synchronous ,则填充1将始终在填充2之前完成。

That's why AJAX means Asynchronous JavaScript Xml, because it is made to run in background. 这就是AJAX表示异步JavaScript Xml的原因,因为它是在后台运行的。

$('#content').html('');
        //$('#LoaderMain').show();
        $.ajax({
           url: "your.url",
           beforeSend: function() {
             //do something before sending the ajax, like hiding or showing that spinner
           },
           type: 'post', //or get if you prefer that
           data: "", // put parameters like ?id=1&name=something here
           success: function(data) {
             //do something after successful ajax request like $('#content').html(data);
           }
        });

What event are you using to load content with? 您正在使用什么事件来加载内容?

http://api.jquery.com/load/#callback-function http://api.jquery.com/load/#callback-function

Most jquery event/ajax functions have a callback param that you can send a function to, in order to execute after the event/ajax function has complete processing. 大多数jquery事件/ ajax函数都有一个回调参数,您可以将其发送给该函数,以便在事件/ ajax函数完成处理后执行。

$('#LoaderMain').show();
$('#result').load('ajax/test.html', function() {
  $('#LoaderMain').hide();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM