seekTo,playVideo和pauseVideo功能在YouTube Player API中无效

时间:2011-05-15 12:32:55

标签: javascript html api youtube

我正在使用YouTube播放器API并使用seekTo()功能,但它不起作用。实际上,playVideo()pauseVideo()也不起作用。这是代码:

<script type="text/javascript" src="swfobject.js"></script>
<body>
<div id="divvideo">
<p>You will need Flash 8 or better to view this content.</p>
</div>
<script type="text/javascript">
    var params = { allowScriptAccess: "always" };
    var atts = { id: "video" };
    swfobject.embedSWF(
        "http://www.youtube.com/v/o3nmOw9vKw4?enablejsapi=1&playerapiid=video", "divvideo", "720", "405", "8", null, null, params, atts);

ytplayer = document.getElementById("video");

function play() {
    if (ytplayer) {
        ytplayer.playVideo();
    }

}
</script>
<a href="javascript:void(0);" onclick="play()">Play</a>
</body>

视频出现,但播放超链接不起作用。我该怎么办?

1 个答案:

答案 0 :(得分:2)

在您的JavaScript中,您有:

ytplayer = document.getElementById("video");

但是当你声明你的元素时,你会这样声明:

<div id="divvideo">

看到不匹配?您正在寻找id"video"的HTML元素,但没有一个 - 您的HTML元素的id代替"divvideo"

您需要更改getElementById来电"divvideo",或者将div更改为id="video"