使用jQuery隐藏多个元素并获得一个回调 [英] Hide multiple elements with jQuery and get one callback

查看:160
本文介绍了使用jQuery隐藏多个元素并获得一个回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题非常简单。如果我用jQuery选择两个或多个元素,例如,使用jQuery的fadeOut()函数来隐藏它们,则调用两次回调函数(对于每个元素)。有没有办法只接收一个回调?

The question is pretty straightforward. If I select two or more elements with jQuery and, for example, use jQuery's fadeOut() function to hide them, the callback function is invoked twice (for each element). Is there a way to only receive one callback?

我目前用于执行此任务的代码粘贴在下面。

The code I am currently using to perform this task is pasted below.

$('#element-1, #element-2').fadeOut( 250, function() { /* Callback invoked twice. */ });

之前发布了一个类似的问题( jQuery多个animate()回调),但对于看似简单的问题,解决方案似乎相当复杂。

A similar question has been posted before (jQuery multiple animate() callback), but the solution seems quite complicated for what seems a simple problem.

推荐答案

您可以使用 $。when [docs] 延迟对象):

$.when($('#element-1, #element-2').fadeOut(250)).then(function() {
    // do something
});

DEMO

这适用于任何动画afaik。

This works with any animation afaik.

这篇关于使用jQuery隐藏多个元素并获得一个回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆