gpt4 book ai didi

javascript - 回调函数和 jQuery 的 every 迭代器

转载 作者:行者123 更新时间:2023-12-02 20:23:56 26 4
gpt4 key购买 nike

我在each迭代器之后调用回调函数时遇到困难。

这是一个例子:

<html>
<head>
<script type="text/javascript" src="jquery-1.4.min.js"></script>
<script type="text/javascript" src="move.js"></script>
</head>

<body>
<div>
<div class="ani" style="position:relative; display: inline-block; top:10px; left:0px; width:30px; height:30px; background:#ddd;"></div>
<div class="ani" style="position:relative; display: inline-block; top:10px; left:110px; width:30px; height:30px; background:#ddd;"></div>
<div class="ani" style="position:relative; display: inline-block; top:10px; left:210px; width:30px; height:30px; background:#ddd;"></div>
<div class="ani" style="position:relative; display: inline-block; top:10px; left:310px; width:30px; height:30px; background:#ddd;"></div>
</div>
</body>
</html>

$(document).ready(function(){

$ani = $('div.ani');

var redFlash = function(){
$ani.eq(0).css('background-color','#e35');
};

var goingDown = function(){
$ani.each(function(i){
$(this).animate({'top':'100px'}, (i+1)*2000);
}),redFlash()};

goingDown();

});

但是redFlash函数没有被调用。我也尝试过:

var goingDown = function(){
$ani.each(function(i){
$(this).animate({'top':'100px'}, (i+1)*2000);
}),function(){
$ani.eq(0).css('background-color','#e35');
};
};

没有效果。

如何让 redFlash 在每个迭代器内的所有动画完成后运行?有没有办法可以在动画完成后对 redFlash 进行 goingDown 回调?

另外,有谁知道关于 javascript/jquery 回调函数的任何好的在线资源或书籍吗?我的书在这个主题上有点不稳定。

编辑:使用 Pointy 的指针使用计数器解决。

$(document).ready(function(){

$ani = $('div.ani');

var redFlash = function(){
$ani.eq(0).css('background-color','#e35');
};

var ani_size = $ani.length-1;
console.debug(ani_size);

var goingDown = function(){
$ani.each(function(i){
$(this).animate({'top':'100px'}, (i+1)*2000, function(){
console.debug(i);
if (i == ani_size){
redFlash();
}
});
});
};
goingDown();

});

最佳答案

页面上每个元素的“id”属性必须唯一。您可能应该使用元素的“类”而不是“id”来表征它们。

关于javascript - 回调函数和 jQuery 的 every 迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5127269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com