【发布时间】:2015-03-03 01:02:48
【问题描述】:
我正在尝试设置一个 videoId 数组,以便能够从中进行选择以随机加载。我尝试将视频数组添加为 videoId,但没有成功。我是一个 JS 新手,所以我不确定我应该为此传回什么。我做到了这一点:
<div id="player"></div>
<script>
function rotateYT() {
var videos = [
'ZMnjkcvjN-E',
'RFQfSMbLCWw',
];
var index=Math.floor(Math.random() * videos.length);
}
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '300',
width: '300',
videoId: videos[index], //where I'm trying to get the random videos
events: {
'onReady': onPlayerReady,
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
event.target.mute();
event.target.setPlaybackQuality('small');
}
</script>
【问题讨论】:
-
你从不调用函数,函数也不返回任何东西。
-
变量
videos和index是rotateYT的局部变量,不能在onYouTubeIframeAPIReady中使用。
标签: javascript arrays youtube-javascript-api