当我在YouTube视频加载时输入回调时,onYouTubePlayerReady
,在jQuery ready函数中,加载视频时不会调用它。 但是,当我将回调放在 jQuery ready 函数之外时,它会被调用。 我该如何修复它,以便我可以将回调放在 jQuery 函数中。 下面的代码。
<html>
<body>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}swfobject.js"></script>
<script type="text/javascript">
/// --> When I put onYouTubePlayerReady here it IS called //
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
ytplayer.playVideo();
}
$(document).ready(function(){
/// --> When I put onYouTubePlayerReady here it is NOT called //
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/UkhisRY3RRQ?version=3&enablejsapi=1","ytapiplayer", "800", "500", "8", null, null, params, atts);
});
</script>
<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
</body>
</html>
另外,这是swfobject的网址,http://swfobject.googlecode.com/svn/trunk/swfobject/swfobject.js 以防万一。
您必须使其全局可用 - 如果将其放在就绪处理程序中,则其范围限定为该处理程序。 你为什么需要它在那里? 您需要访问范围吗? 你可以试试这个,我不确定它是否适合你 - 这取决于何时建立 youtubeplayer 引用指针关联:
<html>
<body>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}swfobject.js"></script>
<script type="text/javascript">
$(document).ready(function(){
onYouTubePlayerReady = function(playerId) {
ytplayer = document.getElementById("myytplayer");
ytplayer.playVideo();
}
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/UkhisRY3RRQ?version=3&enablejsapi=1","ytapiplayer", "800", "500", "8", null, null, params, atts);
});
</script>
<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
</body>
</html>