3

我正在通过 youtube API 加载一个 youtube 视频,因为我想在视频结束时执行一行 jQuery。出于某种原因,我的代码将无法执行,有人能指出我正确的方向吗...

首先我嵌入 iFrame

 <iframe id="player" class="blo" width="219" height="207" src="http://www.youtube.com/embed/qNL81Khx_UE?enablejsapi" frameborder="0" allowfullscreen></iframe>

然后我创建 API 脚本

<script type="text/javascript">
          // 2. This code loads the IFrame Player API code 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);

          // 3. This function creates an <iframe> (and YouTube player)
          //    after the API code downloads.
          var player;
          function onYouTubePlayerAPIReady() {
            player = new YT.Player('player', {
              height: '390',
              width: '640',
              videoId: 'qNL81Khx_UE',
              events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
              }
            });
          }
          </script>

然后我做一个函数

<script type="text/javascript">
 var myPlayerState;
  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING && !done) {
      alert('Something Happened');
    }
    myPlayerState = event.data;
     }

if (myPlayerState == 1){
alert('Something Happened');
}

谁能让我知道我做错了什么?

谢谢!

4

0 回答 0