带有多个视频的Youtube javascript API(和ASP)

Youtube javascript API (& ASP) with multiple videos

本文关键字:API ASP javascript Youtube 视频      更新时间:2023-09-26

我让ASP在我的页面上生成嵌入代码,如下所示:

Table1.Rows(0).Cells(0).Text = "<object style=""height: 390px; width: 640px"" id=""ytplayer_object_left"">" & _
        "<param name=""movie"" value=""https://www.youtube.com/v/UASyS-jUKKI?version=3&feature=player_embedded&controls=0&enablejsapi=1&showinfo=0&iv_load_policy=3&playerapiid=ytplayer_left"">" & _
        "<param name=""allowfullscreen"" value=""true"">" & _
        "<param name=""allowscriptaccess"" value=""always"">" & _
        "<embed src=""https://www.youtube.com/v/UASyS-jUKKI?version=3&feature=player_embedded&controls=0&enablejsapi=1&showinfo=0&iv_load_policy=3&playerapiid=ytplayer_left"" type=""application/x-shockwave-flash"" allowfullscreen=""true"" allowscriptaccess=""always"" width=""425"" height=""344"" id=""ytplayer_left""></object>"

    Table1.Rows(0).Cells(2).Text = "<object style=""height: 390px; width: 640px"" id=""ytplayer_object_right"">" & _
        "<param name=""movie"" value=""https://www.youtube.com/v/uasys-jukki?version=3&feature=player_embedded&controls=0&enablejsapi=1&showinfo=0&iv_load_policy=3&playerapiid=ytplayer_right"">" & _
        "<param name=""allowfullscreen"" value=""true"">" & _
        "<param name=""allowscriptaccess"" value=""always"">" & _
        "<embed src=""https://www.youtube.com/v/uasys-jukki?version=3&feature=player_embedded&controls=0&enablejsapi=1&showinfo=0&iv_load_policy=3&playerapiid=ytplayer_right"" type=""application/x-shockwave-flash"" allowfullscreen=""true"" allowscriptaccess=""always"" width=""425"" height=""344"" id=""ytplayer_right""></object>"

我已经确保两者的ID不同,并且为两者正确调用了onYouTubePlayerReady(id)(返回"ytplayer_left"和"ytplayer_right")。但是,我无法将每个视频作为不同的javascript变量获取,因此我可以独立播放/暂停它们。

这是我尝试过的代码:

var vid_left;
    var vid_right;
    function onYouTubePlayerReady(id) {
        alert(id);
        if (id = 'ytplayer_right') {
            vid_right = document.getelementbyId(id);
            alert(vid_right);
        }
        if(id = 'ytplayer_left') {
            vid_left = document.getelementbyId(id);
            alert(vid_left);
        }

有什么帮助吗?谢谢。

两个问题:

  • 使用document.getElementById(带大写字母)。
  • 使用 == 比较变量而不是=

法典:

var vid_left, vid_right;
function onYouTubePlayerReady(id) {
    alert(id);
    if (id == 'ytplayer_right') {
        vid_right = document.getElementById(id);
        alert(vid_right);
    } else if(id == 'ytplayer_left') {
        vid_left = document.getElementById(id);
        alert(vid_left);
    }
}

我不确定你能用getElementById获得标签。尝试:

var object = document.getElementById(objectID)
var embed = object.getElementsByTagName('embed')[0];