【问题标题】:onYouTubePlayerReady is not firing when the page is loaded with ajax使用 ajax 加载页面时不会触发 onYouTubePlayerReady
【发布时间】:2012-09-21 17:09:27
【问题描述】:

我有以下使用 ajax 加载页面的 jQuery 脚本,我想在页面加载时使用 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 函数未触发。知道为什么吗?

【问题讨论】:

  • 这是在网络服务器还是本地主机上?
  • 是的,我知道,它在网络服务器上:)

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


【解决方案1】:

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

试试

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

【讨论】:

  • 在我将所有代码包装在一个自调用匿名函数中并且无法弄清楚该事件触发发生了什么之后,这救了我。非常感谢。
猜你喜欢
  • 2011-09-02
  • 1970-01-01
  • 2020-08-28
  • 2011-01-06
  • 2012-07-29
  • 2013-01-18
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
相关资源
最近更新 更多