使& # 39;每个# 39;循环连续发生

make 'each' cycles occur consecutively

本文关键字:连续 循环 每个      更新时间:2023-09-26

我有两个动作需要应用到一组div上,但是我需要在另一个循环完成之前发生一个循环。

下面是我的代码:
$("div").each(function(){
        //do stuff first
}).each(function(){
        //do stuff next
});

,但目前do stuff next发生在do stuff first结束之前。我能做些什么来阻止这一切吗?

完整脚本

$("div").each(function(){
                    if($(this).html() === "yes"){
                        $(this).fadeOut(time,function(){
                            $(this).parent().height(0);
                        });

                    }
        }).each(function(){
                    if($(this).html() !== "yes"){
                        $(this).parent().height(25);
                        $(this).fadeIn(time);
                    }
        });

知道你想要淡出,然后设置高度,这会做你所需要的吗?

var divs = $('div');
divs.fadeIn(function () {
  divs.height('200');
});

使用每个允许不同div的不同设置:

$('div').each(function () {
  var div = $(this), toggle = true;
  div.fadeIn(function () {
    if (toggle = !toggle) {
      div.height('200');
    } else {
      div.width('200');
    }
  });
});

看到你的代码片段,我相信我现在明白了:

var yesDivs = $('div').filter(function () {
  return $(this).html() === 'yes';
});
yesDivs.fadeOut(time, function () {
  yesDivs.parent().height(0);
  $('div').filter(function () {
    return $(this).html() !== 'yes';
  }).fadeIn(time).parent().height(25);
});