如何隐藏多个项目,但只调用一次处理程序? [英] how to hide multiple items but only call the handler once?

查看:58
本文介绍了如何隐藏多个项目,但只调用一次处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要隐藏多个这样的项目:

I am hiding multiple items like this:

$('#item1,#item2,#item3').hide('slow',function() {
    console.log('hidden');
});

问题是此日志记录了四次.由于无法控制的原因,我无法在每个元素上使用通用类.有没有一种很好的jquery-ish方式使此处理程序仅在隐藏最后一个项目后才触发? (或第一个,因为它们实际上应该是同时发生的)

Problem is that this logs four times. For reasons beyond my control I cannot use a common class on each element. Is there a nice jquery-ish way to make this handler only fire after the last item has hidden? (or the first, since they should be virtually simultaneous)

此外,这些元素将被隐藏并显示很多次,因此,用于限制处理程序调用的任何内容都必须在此后重置.

Also, the elements will be hidden and shown many times, so whatever is used to limit the handler call will have to be reset afterwards.

我显然可以在处理程序中放入某种布尔值,但是我希望有一个更干净的解决方案.

I can obviously put some kind of boolean in the handler, but I was hoping for a cleaner solution.

推荐答案

您可以使用 $ .when( ) deferred.done()

$.when($('#item1,#item2,#item3').hide('slow')).done(function() {
   console.log('hidden');
});

简单的工作示例

这篇关于如何隐藏多个项目,但只调用一次处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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