[英]How to pass data to hide and show “complete” functions
I need to do something like this: 我需要做这样的事情:
for (var i in arrayOfObjects) {
var options = arrayOfObjects[i];
$('.' + options.className).hide('middle', function(){
//And here I need to use a data from options.
//How can I pass 'options' object here?
$(options.attribute).doSomethig();
});
}
It is because you are using a closure variable options
in an asynchronous callback method. 这是因为您在异步回调方法中使用了关闭变量options
。 One solution is to create a private closure inside the loop as below 一种解决方案是在循环内部创建私有闭包,如下所示
for (var i in arrayOfObjects) {
(function(options){
$('.' + options.className).hide('middle', function () {
//And here I need to use a data from options.
//How can I pass 'options' object here?
$(options.attribute).doSomethig();
});
})(arrayOfObjects[i])
}
Another solution is to iterate using jQuery 另一个解决方案是使用jQuery进行迭代
$.each(arrayOfObjects, function(i, options){
$('.' + options.className).hide('middle', function () {
//And here I need to use a data from options.
//How can I pass 'options' object here?
$(options.attribute).doSomethig();
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.