You can do something like this...
Give every iframe a class so that it can be identified as an iframe for youtube player.
Here I have given "yt_players"
Now you can use the below code...
<script type="text/javascript">
players = new Array();
function onYouTubeIframeAPIReady() {
var temp = $("iframe.yt_players");
for (var i = 0; i < temp.length; i++) {
var t = new YT.Player($(temp[i]).attr('id'), {
events: {
'onStateChange': onPlayerStateChange
}
});
players.push(t);
}
}
onYouTubeIframeAPIReady();
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
var temp = event.target.a.src;
var tempPlayers = $("iframe.yt_players");
for (var i = 0; i < players.length; i++) {
if (players[i].a.src != temp)
players[i].stopVideo();
}
}
}
</script>
Have updated the code...This should be of help to you.
See it in here...http://jsfiddle.net/anubhavranjan/Y8P7y/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…