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

编程入门 行业动态 更新时间:2024-10-05 03:23:09
本文介绍了如何显示jQuery中的加载微调器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Prototype 中,我可以使用以下代码显示正在加载..."图像:

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 () { ... }

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

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

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

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

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

推荐答案

有两种方法.我的首选方式是将一个函数附加到元素本身上的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(); });

更多推荐

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

本文发布于:2023-11-03 04:42:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!