Youtube iframe API调用以播放视频/pauseVideo don'我不在IE8工作

Youtube iframe API calls to playVideo/pauseVideo don't work in IE8

本文关键字:工作 IE8 don pauseVideo 调用 API iframe 播放 视频 Youtube      更新时间:2023-09-26

示例代码:

<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