onYouTubeIframeAPIReady函数没有调用 [英] onYouTubeIframeAPIReady function is not calling

查看:274
本文介绍了onYouTubeIframeAPIReady函数没有调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用 onYouTubeIframeAPIReady 函数,但这不会触发。我在控制台中只获得 frameID ,但其他功能未被调用。

I want to call onYouTubeIframeAPIReady function but this is not firing. I am getting only frameID in console but other functions are not getting called.

$(document).ready(function(){
    var player;
    var ytsrc = $('.video_holder_frame').find('iframe').attr('src');
    if (ytsrc) ytsrc = ytsrc.match(/youtube.com\/embed\/([^\\?]*)/i);
    if (!ytsrc) return;
    var frameID = 'youtube_' + ytsrc[1];    
    console.log(frameID);
    $('.video_holder_frame').find('iframe').attr('id', frameID);

    function attachToYoutubeFrame() {
        console.log("attachToYoutubeFrame");
        function onytplayerStateChange(newState) {
            console.log("onytplayerStateChange");
            console.log(newState);
        };

        player = new YT.Player(frameID, {
            events: {
                "onStateChange": onytplayerStateChange
            }
        });
    };

    function onYouTubeIframeAPIReady() {
        attachToYoutubeFrame();
    };
});


推荐答案

你的onYouTubeIframeAPIReady()函数必须全局定义。只需更换行

Your onYouTubeIframeAPIReady() function must be defined globaly. Simply replace the line

function onYouTubeIframeAPIReady() {

with

window.onYouTubeIframeAPIReady = function() {

加载youtube iframe api库文件也很重要:

It is also important to load the youtube iframe api library file:

<script type="text/javascript" src="https://www.youtube.com/iframe_api"></script>

此外,您的iframe src网址必须附加enablejsapi = 1参数:

Also your iframe src url must have appended the enablejsapi=1 parameter:

http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1

这篇关于onYouTubeIframeAPIReady函数没有调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆