use*_*395 2 javascript jquery youtube-api
我想调用onYouTubeIframeAPIReady
函数,但这不是解雇.我只是frameID
在控制台,但其他功能没有被调用.
$(document).ready(function(){
var player;
var ytsrc = $('.video_holder_frame').find('iframe').attr('src');
if (ytsrc) ytsrc = ytsrc.match(/youtube.com\/embed\/([^\\?]*)/i);
if (!ytsrc) return;
var frameID = 'youtube_' + ytsrc[1];
console.log(frameID);
$('.video_holder_frame').find('iframe').attr('id', frameID);
function attachToYoutubeFrame() {
console.log("attachToYoutubeFrame");
function onytplayerStateChange(newState) {
console.log("onytplayerStateChange");
console.log(newState);
};
player = new YT.Player(frameID, {
events: {
"onStateChange": onytplayerStateChange
}
});
};
function onYouTubeIframeAPIReady() {
attachToYoutubeFrame();
};
});
Run Code Online (Sandbox Code Playgroud)
您的onYouTubeIframeAPIReady()函数必须全局定义.只需更换线路即可
function onYouTubeIframeAPIReady() {
Run Code Online (Sandbox Code Playgroud)
同
window.onYouTubeIframeAPIReady = function() {
Run Code Online (Sandbox Code Playgroud)
加载youtube iframe api库文件也很重要:
<script type="text/javascript" src="https://www.youtube.com/iframe_api"></script>
Run Code Online (Sandbox Code Playgroud)
你的iframe src url也必须附加enablejsapi = 1参数:
http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1
Run Code Online (Sandbox Code Playgroud)