在原型中,我可以用下面的代码显示“加载…”图像:

var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, 
onLoading: showLoad, onComplete: showResponse} );

function showLoad () {
    ...
}

在jQuery中,我可以将服务器页面加载到一个元素中:

$('#message').load('index.php?pg=ajaxFlashcard');

但是我如何附加一个加载旋转到这个命令,因为我在原型?


当前回答

这对我来说是最好的方法:

jQuery:

$(document).ajaxStart(function() {
  $(".loading").show();
});

$(document).ajaxStop(function() {
  $(".loading").hide();
});

咖啡:

  $(document).ajaxStart ->
    $(".loading").show()

  $(document).ajaxStop ->
    $(".loading").hide()

文档: ajaxStart, ajaxStop

其他回答

您可以在AJAX调用之前将动画图像插入到DOM中,并执行内联函数将其删除…

$("#myDiv").html('<img src="images/spinner.gif" alt="Wait" />');
$('#message').load('index.php?pg=ajaxFlashcard', null, function() {
  $("#myDiv").html('');
});

这将确保你的动画在随后的请求中从相同的帧开始(如果这很重要的话)。注意,旧版本的IE可能在动画方面有困难。

好运!

使用加载插件:http://plugins.jquery.com/project/loading

$.loading.onAjax({img:'loading.gif'});

如果你正在使用$.ajax(),你可以使用这样的东西:

$.ajax({
  url: "destination url",
  success: sdialog,
  error: edialog,
  // shows the loader element before sending.
  beforeSend: function() {
    $("#imgSpinner1").show();
  },
  // hides the loader after completion of request, whether successfull or failor.             
  complete: function() {
    $("#imgSpinner1").hide();
  },
  type: 'POST',
  dataType: 'json'
});

虽然设置名为“beforeSend”,但从jQuery 1.5开始,无论请求类型如何,都将调用“beforeSend”。例如,如果输入:'GET', .show()函数将被调用。

$('#message').load('index.php?pg=ajaxFlashcard', null, showResponse);
showLoad();

function showResponse() {
    hideLoad();
    ...
}

http://docs.jquery.com/Ajax/load#urldatacallback

如果你不想自己写代码,也有很多插件可以做到这一点:

https://github.com/keithhackbarth/jquery-loading http://plugins.jquery.com/project/loading