gpt4 book ai didi

javascript - 需要将YouTube视频加载到单独的HTML页面中

转载 作者:行者123 更新时间:2023-12-03 06:12:31 25 4
gpt4 key购买 nike

我已经在首页中加载了播放列表项,如下所示:

HTML:

<div id="youtube-playlist-feed_1"></div>
<div id="youtube-playlist-feed_2"></div>
<div id="youtube-playlist-feed_3"></div>

我要加载播放列表的脚本是:
            var htmlString = "";
var apiKey = 'AIzaSyDI4rWo_wVAxRZEIgF6_8sRZDj8OCOZZ38';
var playlistID = 'PLBhKKjnUR0XBVrHvtDOylN5XREHh9X1nt';
var maxResults = 7;

var playlists = [{
playlistId: 'PLJYHm_ZxWCKnQmapkDs7x47jkr-nw3l50',
el: '#youtube-playlist-feed_1'
},
{
playlistId: 'PLJYHm_ZxWCKmIBkoopKFK4kTTOmC1Zof0',
el: '#youtube-playlist-feed_2'
},
{
playlistId: 'PLBhKKjnUR0XAM2Wvi7JY5gLRpFLzIE-An',
el: '#youtube-playlist-feed_3'
}
];

playlists.forEach(function(playlist) {
getVideoFeedByPlaylistId(playlist.playlistId, playlist.el);
})

function getVideoFeedByPlaylistId(playlistId, el) {
$.getJSON('https://www.googleapis.com/youtube/v3/playlistItems?key=' + apiKey + '&playlistId=' + playlistId + '&part=snippet&maxResults=' + (maxResults > 50 ? 50 : maxResults), function(data) {
$.each(data.items, function(i, item) {
var videoID = item['snippet']['resourceId']['videoId'];
var title = item['snippet']['title'];
var videoURL = 'https://www.youtube.com/watch?v=' + videoID + '&list=' + playlistID + '&index=1';
htmlString += '<div class="video-wrap"><div class="video"><a target="_blank" href="' + videoURL + '"><img src="https://i.ytimg.com/vi/' + videoID + '/mqdefault.jpg"></a></div>' + '<div class="title"><a target="_blank" href="' + videoURL + '">' + title + '</a></div></div>';
});
$(el).html(htmlString);

htmlString = '';
});
}

现在,我有一个单独的页面,即Player.html,我想要一个YouTube Player并加载视频。例如,如果用户单击播放列表的第一项,则应将其加载到我拥有YouTube播放器的Player.html中。我该如何实现?

最佳答案

在您要加载播放列表项目的脚本中,只需将此代码替换为 htmlString

 htmlString += '<div class="video-wrap"><div class="video"><a target="_blank" href="#" onclick="player()"><img src="https://i.ytimg.com/vi/' + videoID + '/mqdefault.jpg"></a></div>' + '<div class="title"><a target="_blank" href="#" onclick="player()">' + title + '</a></div></div><input type="text" name="videoid" id="videoid" value="'+videoID+'" hidden>';

Index.html 文件的末尾:

过去这段代码:
<script type="text/javascript">
function player() {

var ID = document.getElementById('videoid').value;

document.location.href = 'player.html?vid='+ID;

}
</script>

然后在你的 player.html文件中
<div id="display"></div>

<script type="text/javascript">
window.onload = function () {
var url = document.location.href,
params = url.split('?')[1].split('&'),
data = {}, tmp;
for (var i = 0, l = params.length; i < l; i++) {
tmp = params[i].split('=');
data[tmp[0]] = tmp[1];
}
var videoId = data.vid;
var output = '<iframe width="420" height="315" src="https://www.youtube.com/embed/'+videoId+'"></iframe>';
document.getElementById('display').innerHTML = output;
}
</script>

关于javascript - 需要将YouTube视频加载到单独的HTML页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49486840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com