如何获取CoffeeScript .each循环的索引?我到处搜索,似乎找不到可靠的答案。我知道如何使用 Vanilla jQuery做到这一点,但是我不知道如何在CoffeeScript中将index参数添加到function()。
这是我目前的代码:
video_list_element = $('#video-list li')
video_list_element.each ->
video_list_element.delay(100).animate({
"top": "0"
}, 2000)
我正在尝试将.delay()中的值乘以.each循环的索引
非常感谢您的帮助,我非常感谢!!!
问候,
提姆
最佳答案
jQuery .each()函数的文档位于:
http://api.jquery.com/each/
video_list_element = $('#video-list li')
video_list_element.each (index, element) ->
element.delay(100 * index).animate "top": "0", 2000
通常,在(sans-jQuery)中,在coffeescript for循环中获取索引的方法是:
array = ["item1", "item2", "item3"]
for value, index in array
console.log index, value
给出:
0 item1
1 item2
2 item3