Javascript for 循环中的多个计数器 [英] Multiple counters in Javascript for loop

查看:35
本文介绍了Javascript for 循环中的多个计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我扯掉头发并在调试方面走错路之前.有人可以确认此代码会按预期执行.IE.将 5 个 div 动画到不同的位置:

Before i tear my hair out and go down the wrong path in terms of debugging. Can someone confirm that this code will do as intended. Ie. animate 5 divs to different positions:

var i, j, k;
$('#menuButton').click(function(){
    for (i=1; j=0; k=150; i<=5; i++; j+=30; k-=30){
        $('.spinner #item' + i).animate({
            left: '+=' + j,
            bottom: '+=' + k
          }, 500, function() {
            // Animation complete.
        });
    }
});

当我单击 #menuButton 时,没有任何反应,我收到此错误:

When i click the #menuButton, nothing happens and I get this error:

Uncaught SyntaxError: Unexpected token ;在for()"行...

Uncaught SyntaxError: Unexpected token ; on the 'for()' line...

推荐答案

有些分号需要逗号:

for (i=1, j=0, k=150; i<=5; i++, j+=30, k-=30) { /* do work */ }

你的 for

for( [init]; [test]; [increments]) { [block] }

要执行多个 [inits][increments] 您有时必须使用 神奇但经常被遗忘的逗号运算符

To do multiple [inits] or [increments] you have to use the sometimes magical, but oft forgotten, comma operator

这篇关于Javascript for 循环中的多个计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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