Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

jquery - 如何显示jQuery中的加载微调器?(How to show loading spinner in jQuery?)

In Prototype) I can show a "loading..." image with this code:

(在原型中,)我可以使用以下代码显示“正在加载...”图像:)

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

function showLoad () {
    ...
}

In jQuery) , I can load a server page into an element with this:

(在jQuery中) ,我可以使用以下命令将服务器页面加载到元素中:)

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

but how do I attach a loading spinner to this command as I did in Prototype?

(但是如何像在Prototype中一样将加载微调器附加到此命令?)

  ask by Edward Tanguay translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a couple of ways.

(有两种方法。)

My preferred way is to attach a function to the ajaxStart/Stop events on the element itself.

(我的首选方法是将一个函数附加到元素本身上的ajaxStart / Stop事件。)

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

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

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

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

(更新 :从jQuery 1.8开始,文档指出.ajaxStart/Stop应该仅附加到document 。)

This would transform the above snippet to:

(这会将上面的代码段转换为:)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...