0

我有一个数组,

[0][video object]
[0][state]
[0][parameters]

[1][video object]
[1][state]
[1][parameters]
.
.
.

我想知道可以在哪里播放数组中的所有视频:

for(i=0;i<videos.length;i++){
    $(videos[i]['video']).on('canplaythrough', function(){

       //do stuff - check all
       update parent > ['state'] to true;

    }
}

问题是在我松开索引 i 之后。我如何在 on() 函数中传递它?我想获取数组项的父项。

4

2 回答 2

2

You are loosing the value of i because it is a closure variable, in the given case since you are iterating through an array I think it is better to use $.each() here

$.each(videos, function(idx, video){
    $(video.video).on('canplaythrough', function(){

       //do stuff - check all
       video.state = true

    }
})
于 2013-11-01T07:07:29.447 回答
1

to use the var i inside your on function you can do the following

for(i=0;i<videos.length;i++){
    $(videos[i]['video']).on('canplaythrough', function(i){
       console.log(i); // i is accessible here now
       //do stuff - check all
       videos[i]['state'] = true;

    }
}
于 2013-11-01T07:08:16.280 回答