javascript - 以连续的方式执行循环中的所有元素?

标签 javascript jquery css

如何在循环中执行一个函数,使循环中的下一个元素仅在前一个元素完成后才执行。

$(".welcome>p").each(function() {
    var $this = $(this);
    setTimeout(function() {
        $this.animate({
            opacity: 1
        }, 600);
    });

});

因此,此代码中的所有 p 元素将同时运行。我怎样才能改变它,使 p 元素按照它的顺序一个一个地出现?

谢谢, 向L

最佳答案

each 的第一个参数是 index,因此您可以使用该索引来增加 setTimeout,例如:

$(".welcome>p").each(function( idx ) {
    var $this = $(this);
    setTimeout(function() {
        $this.animate({
            opacity: 1
        }, 600);
    }, idx * 600);

});

关于javascript - 以连续的方式执行循环中的所有元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25702797/

相关文章:

JQuery...调整网格偏移中拖动

jquery - 删除 jQuery 中的内联 CSS

html - 将字段集图例居中对齐

javascript - 剑道网格 : how to get the selected item from a Combobox cell template when using with Angular

javascript - 等待数组被ajax结果填充

javascript - 检查 JSON 对象中是否存在日期

css - 跨浏览器@font-face使用

javascript - 如何使容器不超过屏幕 View

javascript - 如何让对应的ul标签可见?

javascript - 在客户端将 wiki 文本(维基词典风格)翻译成 HTML