我正在创建一个网站,我需要外部控件来嵌入Youtube视频
问题是,当它位于onPlayerReady
功能内时,播放按钮不起作用。
这是标记:
<script type="text/javascript" src="https://www.youtube.com/iframe_api"></script>
<div>
<iframe id="player" width="560" height="315" src="https://www.youtube.com/embed/08HqRzhqXU0" frameborder="0" allowfullscreen></iframe>
</div>
<div id="play_btn">PLAY</div>
这是我的剧本:
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady,
}
});
}
function onPlayerReady(event) {
var playButton = document.getElementById("play_btn");
playButton.addEventListener("click", function() {
player.playVideo();
});
}