样例代码:

<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秒。

最佳答案

实际上,IE已经给了您答案。我在IE8模式下测试IE9上的链接并收到错误

对象不支持属性或方法“ addEventListener”

真正的IE8仅支持attachEvent。

您可以检查此选项是否存在并设置正确的侦听器,也可以添加将为您应用此选项的外部脚本,例如在addEventListener Polyfill中。

关于javascript - Youtube iframe API调用playVideo/pauseVideo在IE8中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25508933/

10-11 22:58
查看更多