我想在我的 $(document).ready() 函数中添加 youtube iframe api 代码,但是当我这样做时,播放器似乎没有加载,当我将代码移到 document.ready 之外时,播放器加载正常。任何人都可以就我如何在函数内显示此视频提供任何建议吗?
JS
$(document).ready(function() {
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'u1zgFlCw8Aw',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady() {
console.log('ready');
}
function onPlayerStateChange() {
console.log('player changed');
}
});