JQuery .load() 回调函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3973933/
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
JQuery .load() callback function
提问by Dan
I've seen lots of questions and solutions to problems like this but nothing has worked for me. I have this:
我见过很多类似问题的问题和解决方案,但没有任何效果对我有用。我有这个:
function() {
$("#bdiv").load("bosses.php #icc10n",function(){
return $("#bdiv").html();
});
}
But it's not working. To clarify, I want to load content into #bdiv
and then return the contents of #bdiv
. But it seems that $("#bdiv").html()
is being returned before the content is loaded even though I've put it in a callback function.
但它不起作用。为了澄清,我想内容加载到#bdiv
,然后返回的内容#bdiv
。但似乎$("#bdiv").html()
在加载内容之前返回,即使我已将其放入回调函数中。
回答by Petah
$("#bdiv").load("bosses.php #icc10n",function(data){
// use the data param
// e.g. $(data).find('#icc10n')
});
回答by Danny Bullis
as far as I know you cannot make a returnstatement in the callback function of a $.ajax(), $.post(), $.get(), etc.. method. You could, however, store the 'data' value in a variable declared outside the function, and then set the value of that variable when the callback function executes. And there is a variety of other options.
据我所知,您不能在 $.ajax()、$.post()、$.get() 等方法的回调函数中创建return语句。但是,您可以将“数据”值存储在函数外部声明的变量中,然后在回调函数执行时设置该变量的值。还有多种其他选择。
回答by SLaks
You can't do that.
你不能那样做。
AJAX is asynchronous, meaning that your function will return before the server sends a response.
You need to pass the value to your caller using a callback, the way $.load
does.
AJAX 是异步的,这意味着您的函数将在服务器发送响应之前返回。
您需要使用回调将值传递给您的调用者,方法$.load
确实如此。