2012-04-20 3 views
3

프로그래밍 방식으로 중지해야하는 HTML이 포함 된 iframe이 내장되어 있지만 iframe의 ID를 설정할 수 없습니다. 따라서 DOM 요소를 docs에 지정된대로 iframe ID 대신 YT.Player 생성자에 전달하여 YouTube 개체를 만들려고합니다. 나는 또한 '? enablejsapi = 1'을 iframe src url 끝에 추가했습니다.DOM 객체를 전달하여 HTML의 iframe에서 YT.Player 객체를 만듭니다.

내가 다음 코드를 사용하여 내 JS 파일의 상단에있는 유튜브 JS API를로드 한

: 나는 다음과 같은 코드가 그 후

var tag = document.createElement('script'); 
tag.src = "http://www.youtube.com/player_api"; 
var firstScriptTag = document.getElementsByTagName('script')[0]; 
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 
function onYouTubePlayerAPIReady(){ console.log('yt api ready'); } 

는 :

$(function(){ 
    $('a.close').click(function(){ 
     var player = new YT.Player($('iframe').get(0)); 
     player.stopVideo(); 
    }); 
}) 

I 출력을 얻을 :

yt api ready 
Uncaught TypeError: Object [object Object] has no method 'stopVideo' 

나는 어떻게 든 개체 인스턴스화를 속이고 있다고 가정하지만, 어떻게 해야할지 모르겠다. 올바르게. iframe id 생성자를 전달하지 않고 YT.Player 객체를 어떻게 만듭니 까? 어떤 도움을 주셔서 감사합니다.

답변

3

정지 화상 통화를 플레이어 개체의 준비 이벤트로 이동해야했습니다. 다음과 같은 변경이 나를

추가를 위해 다음과 같은 기능을 고정 :

function onMyPlayerReady(event) { event.target.stopVideo(); } 

을 그리고에 yt.player 생성자를 변경 :

new YT.Player($('iframe').get(0), { events: { 'onReady': onMyPlayerReady } }); 
+1

당신이 당신의 전체 코드를 게시 할 수있는 기회? yt.player 생성자를 배치하는 위치를 잘 모르겠습니다. 클릭 이벤트 내에서? – helgatheviking

관련 문제