1
任何想法爲什麼這個腳本不起作用?我想要的只是跟蹤onStateChanged
事件,但這從來都不是被稱爲。YouTube onPlayerReady永遠不會被觸發
當我用下面的代碼打開html文檔時,我沒有錯誤,youtube腳本加載得很好,播放器對象不是undefined
,看起來也不錯。
$(document).ready(function(){
loadScript();
});
function loadScript() {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
alert(player);
}
var done = false;
function onPlayerStateChange(event) {
alert('state changed');
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 3000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
function onPlayerReady(event) {
event.target.playVideo();
}
我iframe
:
<iframe id="player width="400" height="300" src="https://www.youtube.com/embed/j0pJekWgeFE" frameborder="0" allowfullscreen></iframe>
見琴:https://jsfiddle.net/rg2uy1f8/
你試過宣佈它像'window.onYouTubeIframeAPIReady(){(...)}'? – BoltKey