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

Javascript 在Ajax.load()期间显示/隐藏Div,javascript,jquery,Javascript,Jquery,我想在.load()之前显示“LoaderMain”div,并在完成后隐藏“LoaderMain”。当我取消注释“显示/隐藏”时,它不会显示 $('#content').html(''); //$('#LoaderMain').show(); $("#content").load(url, function(response, status, xhr) { if (status == "error") {

我想在.load()之前显示“LoaderMain”div,并在完成后隐藏“LoaderMain”。当我取消注释“显示/隐藏”时,它不会显示

$('#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();
放置
$('#LoaderMain').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').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();
});
放置
$('#LoaderMain').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').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();
});

由于加载是异步的,因此在回调中需要使用hide()函数:


由于加载是异步的,因此在回调中需要使用hide()函数:

加载是异步的,这意味着函数加载将启动,当它运行时,脚本继续执行stuff 2,一旦后台运行的函数完成,它将执行stuff 1。请注意,如果函数非常快,那么可以先完成stuff 1,再完成stuff 2

如果函数是同步的,那么stuff 1总是在stuff 2之前完成

这就是为什么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);
           }
        });
加载是异步的,这意味着函数加载将启动,当它运行时,脚本继续执行stuff 2,一旦后台运行的函数完成,它将执行stuff 1。请注意,如果函数非常快,那么可以先完成stuff 1,再完成stuff 2

如果函数是同步的,那么stuff 1总是在stuff 2之前完成


这就是为什么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);
           }
        });

大多数jquery事件/ajax函数都有一个回调参数,您可以将函数发送到该参数,以便在事件/ajax函数完成处理后执行

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

您使用什么事件加载内容

大多数jquery事件/ajax函数都有一个回调参数,您可以将函数发送到该参数,以便在事件/ajax函数完成处理后执行

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

你必须在回调中隐藏它。你必须在回调中隐藏它。