4

我目前有以下代码可以在页面上播放 youtube 视频。

<div id="player"></div>
<script>
//Load player api asynchronously.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'JW5meKfy3fY',
      playerVars: { 'autoplay': 1, 'rel': 0, 'showinfo': 0, 'egm': 0, 'showsearch': 0, },
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
}
function onPlayerReady(event) {
    event.target.playVideo();
}
function onPlayerStateChange(event) {
    //When the video has ended
    if (event.data == YT.PlayerState.ENDED) {
        //Get rid of the player
    }
}

onPlayerStateChange 函数找出播放器何时播放完视频。那么我怎样才能让这个播放器消失呢?

4

2 回答 2

5

如果您使用的是 IFRAME API:

function onPlayerStateChange(event) {
    //When the video has ended
    if (event.data == YT.PlayerState.ENDED) {
        //Get rid of the player
        event.target.destroy();
    }
}
于 2012-09-21T17:56:00.040 回答
2

也许像

function onPlayerStateChange(event,element) {
    //When the video has ended
    if (event.data == YT.PlayerState.ENDED) {
        //Get rid of the player
        element.style.display = "none";
    }
}
于 2012-05-18T12:20:21.190 回答