jQuery 如何调用jquery ajaxStart + ajaxComplete
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1430438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call jquery ajaxStart + ajaxComplete
提问by antonanton
I have a load function and would like for the code to write some html into a div while it loads, and when it completes, to display the page. I saw some little writeups on the ajaxStart and ajaxComplete events, however I am not sure how to implement them.
我有一个加载函数,并希望代码在加载时将一些 html 写入 div,并在加载完成时显示页面。我在 ajaxStart 和 ajaxComplete 事件上看到了一些小文章,但是我不确定如何实现它们。
Here is the jquery I am thinking of using, however not sure how to implement within the code I have currently…
这是我正在考虑使用的 jquery,但不确定如何在我目前拥有的代码中实现......
$(document).ajaxStart(function(){
$('#content').html("Loading Content...");
});
Here is the current jquery I am using:
这是我正在使用的当前 jquery:
//Load content
$(".load").click(function(){
$("#content").empty();
loadName = $(this).attr("id");
$("#content").load("/content/" + loadName + ".php");
});
回答by karim79
If it's a single div and you would like to update its contents with a loading message/indicator, you can do so like this:
如果它是单个 div 并且您想使用加载消息/指示器更新其内容,您可以这样做:
$("#content").html('<strong>Loading...</strong>')
.load("/content/" + loadName + ".php");
I wouldn't use the ajaxStart
and ajaxStop
/ajaxComplete
global eventsunless you have a common loading indicator for allajax calls. That said, it can be done as follows:
除非您对所有ajax 调用都有一个通用的加载指示器,否则我不会使用ajaxStart
和ajaxStop
/ajaxComplete
全局事件。也就是说,可以按如下方式完成:
$("#loading").bind("ajaxStart", function(){
$(this).show();
}).bind("ajaxStop", function(){
$(this).hide();
});
where the loading
element is whatever you want to reveal during an ajax call.
其中loading
元素是你想要一个Ajax调用期间透露什么。