Javascript 除一项之外的每个项的数组回调
我有以下数组:Javascript 除一项之外的每个项的数组回调,javascript,jquery,arrays,jwplayer,Javascript,Jquery,Arrays,Jwplayer,我有以下数组: var videos = []; $('.video').each(function(){ videos.push($(this).attr('id')); }); 在这里: events: { onPlay: function() { var id = vid.attr('id'); var index = videos.indexOf(id); videos.splice(index, 1);
var videos = [];
$('.video').each(function(){
videos.push($(this).attr('id'));
});
在这里:
events: {
onPlay: function() {
var id = vid.attr('id');
var index = videos.indexOf(id);
videos.splice(index, 1);
console.log(videos);
}
}
我也试过这样的方法:
// if($(this).attr('id') != $('.video').attr('id') ){
// jwplayer( $('.video').attr('id')).stop();
// }
我想为每个数组项(视频)运行一个函数,数组值除外,该值等于:
vid.attr('id')
我正在使用splice,但这会完全删除该项目,因为我有多个视频,我将以一个空阵列结束。我基本上只想在同一时间播放一个视频,所以我需要停止其他视频
全功能代码:
$(function(){
//add attributes to your ".video" DIV placeholder to customimize output
//example: data-autostart="true" - see customSettings Object below.
if (typeof(jwplayer) !== 'undefined'){
var videos = [];
$('.video').each(function(){
videos.push($(this).attr('id'));
});
$('.video').each(function(){
var vid = $(this),
videoSettings = {},
settings = {
autostart: false,
width: '100%',
aspectratio: '16:9',
image: ''
},
customSettings = {
autostart: vid.attr('data-autostart'),
width: vid.attr('data-width'),
aspectratio: vid.attr('data-aspectratio'),
image: vid.attr('data-image')
};
$.extend(videoSettings, settings, customSettings);
var playerInstance = jwplayer( vid.attr('id') ).setup({
primary: "flash",
file: Drupal.settings.basePath + $(this).attr('data-src'),
autostart: videoSettings.autostart,
aspectratio: videoSettings.aspectratio,
image: Drupal.settings.basePath + videoSettings.image,
skin: "glow",
stretching: "exactfit",
width: videoSettings.width,
events: {
onPlay: function() {
var id = vid.attr('id');
var index = videos.indexOf(id);
videos.splice(index, 1);
console.log(videos);
// if($(this).attr('id') != $('.video').attr('id') ){
// jwplayer( $('.video').attr('id')).stop();
// }
}
}
// ga:{idstring: videoSettings.videotitle,
// trackingobject: "pageTracker"
// }
});
});
}
});
一个简单的forEach怎么样
onPlay: function() {
var id = vid.attr('id');
videos.forEach(function(videoId) {
if(videoId != id) {
//You may want to check first if video is running at all
jwplayer(videoId).stop();
}
});
}
一个简单的forEach怎么样
onPlay: function() {
var id = vid.attr('id');
videos.forEach(function(videoId) {
if(videoId != id) {
//You may want to check first if video is running at all
jwplayer(videoId).stop();
}
});
}