且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 jQuery 中显示加载微调器?

更新时间:2023-11-10 23:48:04

有几种方法.我的首选方法是将函数附加到元素本身的 ajaxStart/Stop 事件.

There are a couple of ways. My preferred way is to attach a function to the ajaxStart/Stop events on the element itself.

$('#loadingDiv')
    .hide()  // Hide it initially
    .ajaxStart(function() {
        $(this).show();
    })
    .ajaxStop(function() {
        $(this).hide();
    })
;

每当您执行任何 Ajax 调用时,ajaxStart/Stop 函数都会触发.

The ajaxStart/Stop functions will fire whenever you do any Ajax calls.

更新:从 jQuery 1.8 开始,文档指出 .ajaxStart/Stop 应该只附加到 document.这会将上述代码段转换为:

Update: As of jQuery 1.8, the documentation states that .ajaxStart/Stop should only be attached to document. This would transform the above snippet to:

var $loading = $('#loadingDiv').hide();
$(document)
  .ajaxStart(function () {
    $loading.show();
  })
  .ajaxStop(function () {
    $loading.hide();
  });