jquery - 页面加载Ajax时不会触发onYouTubePlayerReady

标签 jquery ajax youtube youtube-api youtube-javascript-api

我有以下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();
}

关于jquery - 页面加载Ajax时不会触发onYouTubePlayerReady,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12535035/

相关文章:

javascript - JQuery 发布 JSON 数据抛出错误, "unquoted Identifier ' Id' 在位置 0"

javascript - 点击按钮后如何激活iOS键盘专注于输入?

javascript - 防止将 anchor 链接添加到 URL

jquery - 使用 Ajax 更新 Asp.Net MVC 部分 View

php - 将视频上传到 YouTube 作为不公开

使用 .text() .val() 或 .html() 时 Jquery 对象返回未定义

javascript - Firefox 扩展中的 XMLHttpRequest

javascript - 通过 Ajax 在不同的下拉列表中上传数据

jquery - 如何调整 YouTube 播放器的大小,从缩略图大小调整为 'normal' 大小

android - YouTube API全屏人像模式视频android