为什么这不能将transform:none应用于我的元素?

时间:2015-11-20 23:45:48

标签: javascript jquery html css css3

我在$(document).ready(function() { ... });

中调用了以下内容
var serviceIcons = $('#services-row img');
for (var k = 0, n = serviceIcons.length; k < n; ++k) {
    setTimeout(function () { 
        $(serviceIcons[k]).css('transform', 'none'); 
    }, k * 50);
}

我已经确认$('#services-row img')正在返回正确的对象集,但我没有看到style="transform:none;"上的img属性应用于{ {1}}秒。相应的CSS是:

img

1 个答案:

答案 0 :(得分:3)

当调用超时函数时,serviceIcons.length变量已经完成循环,并且它将始终等于函数内的setTimeout( function(k) { return function() { ... } }(k), k * 50 );

要访问循环值,请改用此语法:

var serviceIcons = $('#services-row img');
for(var k = 0, n = serviceIcons.length; k < n; ++k) {
  setTimeout(
    function(k) {
      return function() {
        $(serviceIcons[k]).css('transform', 'none');
      }
    }(k), k * 50
  );
}

所以你的最终代码是:

select max(days) --The highest day in the table (convert these to int first)
- (sum(candies)  --Total candies purchased
- (select top 1 candies from #a order by days desc)) --Minus the candies purchased on the last day
from MyTable

有关此解决方案的详细说明,请参阅http://brackets.clementng.me/post/24150213014/example-of-a-javascript-closure-settimeout-inside

Fiddle