我有以下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函数未触发。知道为什么吗?
答案 0 :(得分:13)
onYouTubePlayerReady
函数需要位于window
上下文中(以便ActionScript代码在加载后可以调用它),并且函数的定义似乎有一些本地上下文。
尝试
window.onYouTubePlayerReady = function(playerId) {
ytplayer = document.getElementById("ytPlayer");
ytplayer.cueVideoById("ylLzyHk54Z0");
ytplayer.playVideo();
}