我正在使用 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 不是函数
return ytplayer.playVideo();