示例代码:
<div id="ytplayer"></div>
<a id="start" href="#">start</a>
<script>
// Load the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer', {
height: '390',
width: '640',
videoId: 'U6oKcAXiVlo',
playerVars: { 'autoplay': 0, 'controls': 0, 'showinfo': 0 },
});
}
document.getElementById("start").addEventListener("click", function () {
player.playVideo();
});
</script>
可重复的测试用例: http://fiddle.jshell.net/3fpusLm5/show/
播放器变量在范围内。但是调用对象上应该可用的方法(例如playVideo,pauseVideo)不起作用。它说Object does not support this property or method
。
该代码适用于大多数现代浏览器。我确定玩家已经装好了。我测试了onReady回调,并尝试等待30秒。
答案 0 :(得分:0)
实际上,IE已经给你答案了。我在IE8模式下测试IE9上的链接并收到错误
对象不支持属性或方法'addEventListener'
真正的IE8仅支持 attachEvent 。
您可以检查此选项是否存在并设置正确的侦听器,或添加将为您应用此选项的外部脚本,例如 addEventListener Polyfill 。