在原型中,我可以用下面的代码显示“加载…”图像:
var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars,
onLoading: showLoad, onComplete: showResponse} );
function showLoad () {
...
}
在jQuery中,我可以将服务器页面加载到一个元素中:
$('#message').load('index.php?pg=ajaxFlashcard');
但是我如何附加一个加载旋转到这个命令,因为我在原型?
最后我对原来的回复做了两处修改。
从jQuery 1.8开始,ajaxStart和ajaxStop只能附加到文档中。这使得仅过滤某些ajax请求变得更加困难。秀……
切换到ajaxSend和ajaxComplete可以在显示转轮之前检查当前ajax请求。
以下是修改后的代码:
$(document)
.hide() // hide it initially
.ajaxSend(function(event, jqxhr, settings) {
if (settings.url !== "ajax/request.php") return;
$(".spinner").show();
})
.ajaxComplete(function(event, jqxhr, settings) {
if (settings.url !== "ajax/request.php") return;
$(".spinner").hide();
})
JavaScript
$.listen('click', '#captcha', function() {
$('#captcha-block').html('<div id="loading" style="width: 70px; height: 40px; display: inline-block;" />');
$.get("/captcha/new", null, function(data) {
$('#captcha-block').html(data);
});
return false;
});
CSS
#loading { background: url(/image/loading.gif) no-repeat center; }