如何在jQuery中获取Youtube视频

How to get Youtube video in jQuery?

本文关键字:获取 Youtube 视频 jQuery      更新时间:2023-09-26

我的iframe 中有这个嵌入的视频

<iframe id="Frustrated" width="560" height="315" src="https://www.youtube.com/embed/mjQwedC1WzI" frameborder="0" allowfullscreen sandbox="allow-scripts allow-same-origin"></iframe>

对于一个普通的视频,你会得到类似的东西

$('#Frustrated').get(0)

获取视频并用它调用函数。

然而,当我尝试在YouTube视频中这样做时,这是行不通的。我在谷歌开发页面上读到,应该建立API并遵循它。然而,我从来没有使用过API,我发现整个事情非常令人困惑。

这是我目前拥有的jQuery,我正试图在iframe上进行工作。

jQuery(document).ready(function($) {
    var video = $('section iframe').get(0);
    video.onended = function(e) {
      $('section iframe').animate({ width: "50%"}, 'slow', function(){
          $('#information').fadeIn('slow');
      });
    }
    video.onplay = function(e) {
        $('#information').fadeOut('slow', function(){
    $('section iframe').animate({ width: "100%" }, 'slow');
    });
    }
    $('section iframe').click(function() {
        if(this.paused || this.ended){
            $('#information').fadeOut('slow', function(){
           $('section iframe').animate({ width: "100%" }, 'slow', function(){
           video.play();
           });
            });
        } 
        else if (this.play) {
          video.pause();
          $('section iframe').animate({ width: "50%" }, 'slow');
              $('#information').fadeIn('slow');
    }
    });
});

有人能解释如何进行吗?

查看https://developers.google.com/youtube/iframe_api_reference

没有iframehttps://developers.google.com/youtube/js_api_reference?hl=es