我有以下jQuery脚本可使用ajax加载页面,并且我想在加载页面时使用Youtube API显示视频:

$("#games_slides").load($(this).attr('href'), function(){
    function onYouTubePlayerReady(playerId) {
            ytplayer = document.getElementById("ytPlayer");
            ytplayer.cueVideoById("ylLzyHk54Z0");
            ytplayer.playVideo();
    }

    function loadPlayer() {
            var params = { allowScriptAccess: "always" };
            var atts = { id: "ytPlayer" };
            swfobject.embedSWF("http://www.youtube.com/apiplayer?" + "version=3&enablejsapi=1&playerapiid=player1", "youtubePlayer", "480", "295", "9", null, null, params, atts);
    }

    loadPlayer();
});

播放器已加载,因此loadPlayer函数可以使用,但是onYouTubePlayerReady函数无法启动。知道为什么吗?

最佳答案

onYouTubePlayerReady函数需要在window上下文中(以便ActionScript代码在加载后可以调用它),并且该函数的定义似乎具有某些本地上下文。

尝试

window.onYouTubePlayerReady = function(playerId) {
  ytplayer = document.getElementById("ytPlayer");
  ytplayer.cueVideoById("ylLzyHk54Z0");
  ytplayer.playVideo();
}

09-18 21:09