gpt4 book ai didi

javascript - CoffeeScript - 等效的 JS 代码无法播放嵌入式 Youtube 视频

转载 作者:行者123 更新时间:2023-11-29 20:11:57 24 4
gpt4 key购买 nike

我正在使用 swfObject 在网站上嵌入 Youtube 视频,因此我可以使用 Youtube Player API 对其进行操作。但是,我正在用 Coffeescript 重写网站,并且一直卡在这个问题上。

这是 html:

<html>
<head>

<meta charset="UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="js/swfobject.js">//embedding youtube videos</script>
<script type="text/javascript" src="coffee/test.js"></script>

</head>

<body>
<div id="ytplayer">
<p>You will need Flash 8 or better to view this content.</p>
</div>
</body>
<script type="text/javascript">
var params = { allowScriptAccess: "always" };
swfobject.embedSWF( "http://www.youtube.com/v/yeClJneSNXA&enablejsapi=1&playerapiid=ytplayer", "ytplayer", "425", "365", "8", null, null, params);
</script>
</html>

以下代码有效:

ytplayer = document.getElementById('ytplayer');
ytplayer.playVideo();

但是,等效的(我认为)coffeescript 代码不会:

ytplayer = document.getElementById("ytplayer")
ytplayer.playVideo()

编译为以下 JS:

(function() {
var ytplayer;

ytplayer = document.getElementById("ytplayer");

ytplayer.playVideo();

}).call(this);

它在 Firebug 中给我以下错误:

ytplayer.playVideo is not a function
return ytplayer.playVideo();

最佳答案

这很可能是因为在您尝试播放 YouTube 视频之前 YouTube 播放器尚未完成加载。

试试这个:

window.onYouTubePlayerReady = (playerid) ->
ytplayer = document.getElementById(playerid)
ytplayer.playVideo()

这会将您的代码放在 onYouTubePlayerReady callback 中YouTube 用来让您知道播放器已准备就绪。

我假设 JS 代码有效是因为您在 Firebug 中运行它?如果是这样,那将是有道理的,因为当您在控制台中输入该命令时,很可能会加载 YouTube 播放器。

关于javascript - CoffeeScript - 等效的 JS 代码无法播放嵌入式 Youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9165902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com