我正在使用Youtube API,并且有几个Divs作为缩略图。单击一个缩略图(div)后,应播放相应的Youtube视频。我正在为视频使用iframe元素,而网页仅由HTML,CSS和JavaScript组成。如果我不使用按钮(带有onclick的缩略图/ div),而是使用空白页面包含Youtube API JavaScript代码,那么它将起作用。但是,当我尝试将所有内容包装在函数调用中(通过div的onclick)时,什么也没有发生,但是出现了此错误。
这是我的代码:
HTML:
<div id="player"></div>
JavaScript:
// 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: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
//setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
这段代码只是从https://developers.google.com/youtube/iframe_api_reference复制而来。
当我在文档中拥有全部内容时,页面加载时就会有一个播放器在那里,并且视频可以开始播放了。
因此,为澄清我的问题,我该如何做才能使用户单击按钮(div)之一时,Youtube播放器显示并播放视频?
最佳答案
您好,我认为这对您有帮助:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Play Youtube Video On Click</title>
</head>
<body>
<button onclick="myFunction();return false;">Click me</button>
<div id="video"></div>
<script>
function myFunction() {
document.getElementById("video").innerHTML = "<div id='player'></div>";
// 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: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
//setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>