如何检测使用 jQuery 完成的“任何”ajax 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2912057/
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 can I detect 'any' ajax request being completed using jQuery?
提问by Brian Scott
I have a page where I can insert some javascript / jquery to manipulate the output. I don't have any other control over the page markup etc.
我有一个页面,我可以在其中插入一些 javascript/jquery 来操作输出。我对页面标记等没有任何其他控制权。
I need to add an extra element via jquery after each present on the page. The issue is that the elements are generated via an asynchronous call on the existing page which occurs after $(document).ready is complete.
我需要在页面上的每个元素之后通过 jquery 添加一个额外的元素。问题是元素是通过在 $(document).ready 完成后发生的现有页面上的异步调用生成的。
Essentially, I need a way of calling my jquery after the page has loaded and the subsequent ajax calls have completed. Is there a way to detect the completion of any ajax call on the page and then call my own custom function to insert the additional elements after the newly created s ?
本质上,我需要一种在页面加载和后续 ajax 调用完成后调用 jquery 的方法。有没有办法检测页面上任何ajax调用的完成,然后调用我自己的自定义函数在新创建的s之后插入额外的元素?
回答by Nick Craver
Unfortunately this doesn't apply since it seems the OP isn't using$.ajax()
or any jQuery ajax method for actually loading content, but leaving it here in case future googler's are doing this.
不幸的是,这并不适用,因为似乎 OP 没有使用或任何 jQuery ajax 方法来实际加载内容,但将其留在这里以防将来 googler 这样做。$.ajax()
You can use any of the global ajax events that meet your needs here, you're probably after $.ajaxComplete()
or $.ajaxSuccess()
.
您可以在此处使用任何满足您需要的全局 ajax 事件,您可能正在使用 $.ajaxComplete()
或$.ajaxSuccess()
.
For example:
例如:
$(document).ajaxSuccess(function() {
alert("An individual AJAX call has completed successfully");
});
//or...
$(document).ajaxComplete(function() {
alert("ALL current AJAX calls have completed");
});
If you want to run just some generic function then attach them to document (they're just events underneath). If you want to show something in particular, for example a modal or message, you can use them a bit neater (though this doesn't seem to be what you're after), like this:
如果您只想运行一些通用函数,则将它们附加到文档(它们只是下面的事件)。如果你想特别显示一些东西,例如一个模式或消息,你可以更整洁地使用它们(尽管这似乎不是你想要的),像这样:
$("#myModal").ajaxComplete(function() {
$(this).fadeIn().delay(1000).fadeOut();
});
回答by Gutzofter
This example just shows and hides elements at the start and end of ajax calls using jQuery:
这个例子只是在使用 jQuery 的 ajax 调用开始和结束时显示和隐藏元素:
$("#contentLoading").ajaxSend(function(r, s) {
$(this).show();
$("#ready").hide();
});
$("#contentLoading").ajaxStop(function(r, s) {
$(this).hide();
$("#ready").show();
});
#contentLoading
is an gif image progress indicator.
#contentLoading
是一个 gif 图像进度指示器。
回答by Sébastien Brodeur
回答by sanya
As i could understand, you are using some jQuery's Ajax function in your ready handler. So you could just pass it another function, which will be invoked after your Ajax function gets response. For example
正如我所理解的,您在准备好的处理程序中使用了一些 jQuery 的 Ajax 函数。因此,您可以将另一个函数传递给它,该函数将在您的 Ajax 函数获得响应后被调用。例如
$(document).ready(function(){
$("#some_div").load('/some_url/', function(){
/* Your code goes here */
});
});
回答by Tgr
You could use .live()/.delegate().
你可以使用.live()/ .delegate()。