我正在使用Iframe YouTube API处理YouTube播放器。播放器加载视频,但当我尝试使用onclick事件更改视频时,它不会更改视频。这是我的HTML。
<li onclick="changeVideo('Vw4KVoEVcr0')"><a href=""><img class="button" src="" alt="Video Button"> item</a></li>
这是我的JavaScript。
// JavaScript Document
//create script tag for youtube api
var tag = document.createElement("script");
//set the src attribute of the script tag to the youtube api
tag.src = "https://www.youtube.com/iframe_api";
//get the first script tag of the document
var firstScript = document.getElementsByTagName("script")[0];
//insert our youtube api tag before the first script tag in the page
firstScript.parentNode.insertBefore(tag, firstScript);
/*
*
*
*/
//the video player element
var player;
//state of the video being played
var done;
/*
*
*/
function onYouTubeIframeAPIReady(){
//
player = new YT.Player("player", {
width: "848",
height: "400",
videoId: "Unp7GtSPJ0Y",
events: {
"onReady": onPlayerReady,
"onStateChange": onPlayerStateChange
}
});
}
/*
*
*/
function onPlayerReady(event){
event.target.playVideo();
}
/*
*
*/
function onPlayerStateChange(event){
if(event.data == YT.PlayerState.PLAYING && !done){
setTimeout(stopVideo, 6000);
done = true;
}
}
/*
*
*/
function stopVideo(){
player.stopVideo();
}
/*
* Changes the videoId and starts playing the new video
*/
function changeVideo(videoIdentifier){
var vidId = videoIdentifier;
player.loadVideoById(vidId);
}
答案 0 :(得分:1)
在我很少睡觉之后,我只是非常愚蠢。拿走链接。在视频发生变化之前,它会一直将我重定向到新版本的页面。