我有以下问题:
$('#id').on('click', '.class', function(){
// it is doing something and
// executing AJAX request
// with the data from request it is doing something
} // this is onclick handler
稍后在代码中我也需要执行它,执行后我需要运行另一个函数,这取决于 this 的 ajax 执行onclick handler
,所以我正在做这样的事情
$('#id .class').click();
anotherFunction();
它不工作。这个问题是可以理解的,因为ajax请求还没有完成。
我尝试使用Deferred object的想法来实现正确的执行。
$.when( $('#id .class').click() ).then( anotherFunction() );
并使用带有回调的自动执行函数的想法:
(function(o, callback){
$('#id .class').click()
})(null, anotherFunction() );
两种想法都失败了。
有什么方法可以在不修改anotherFunction()
and的情况下实现预期的功能onclick function
?