-1

我想做一个函数,除了当前在数组中单击。意味着我有这样的事情:

cubesmixed = [array, with, 145, elements]
cubesmixed[54].click(function() { 
    for(var i = 0; i < 145; i++) {
    var randomnumber=Math.floor(Math.random()*500000);
    cubesmixed[i].animate({ x: randomnumber, y: randomnumber }, randomnumber, "bounce");
}

我不想cubesmixed[54]被动画化。我该怎么说 cubesmixed[i] except 54

提前致谢!

4

3 回答 3

1

尝试这个:

cubesmixed = [array, with, 145, elements]
cubesmixed[54].click(function() { 
    for(var i = 0; i < 145; i++) {
    var randomnumber=Math.floor(Math.random()*500000);
    if(i!=54)){
        cubesmixed[i].animate({ x: randomnumber, y: randomnumber }, randomnumber, "bounce");
    }
}
于 2013-10-17T10:21:51.087 回答
0

这是我的头顶,所以为不准确的地方道歉:

var cubesmixed = $("elementsYouCanClick");

cubesmixed.each(function(idx, element){
    element.click(function() { 
        for(var i = 0; i < 145; i++) {
            if (i === idx) { 
                continue;
            }

            var randomnumber = Math.floor(Math.random()*500000);

            cubesmixed[i].animate({ x: randomnumber, y: randomnumber }, randomnumber, "bounce");
        }
    }
})

不完全确定,但关键思想是使用该$.each()函数遍历元素,这使您idx可以使用。

于 2013-10-17T10:25:16.367 回答
-1

也许这可以解决问题...

cubesmixed = [array, with, 145, elements]
cubesmixed[54].click(function() { 
  for(var i = 0; i < 145; i++) {
    if(cubesmixed[i] == $(this)) continue;
    var randomnumber=Math.floor(Math.random()*500000);
    cubesmixed[i].animate({ x: randomnumber, y: randomnumber }, randomnumber, "bounce");
  }
}

编辑

将行if(cubesmixed[i] == $(this)) continue;更改为

if(i == cubesmixed.indexOf($(this))) continue;
于 2013-10-17T10:17:08.777 回答