Javascript addEventListener onStateChange 在 IE 中不起作用

我有两个颜色框弹出框,每个框都显示一个 YouTube 视频.当他们完成播放时,我试图让他们自动关闭彩盒窗口.下面的代码在 Firefox 中完美运行,但在 IE 中我无法让 addEventListener 工作.我试过 attachEvent 没有成功.任何人都可以就如何解决这个问题提供任何建议吗?这看起来很简单,但我已经筋疲力尽地试图找到解决方案.

I have two colorbox popup boxes which show a YouTube video in each. When they're finished playing, I'm trying to have them automatically close the colorbox window. This code below works perfect in Firefox, but in IE I can't get addEventListener to work. I've tried attachEvent with no success. Can anybody offer any suggestions as to how to solve this? It seems simple but I'm exhausted trying to find a solution.

更新 1:

嗯,这是我当前的代码.它在 Firefox 中完美运行,但 IE 仅输出良好.IE8 调试器也不报任何错误...

Well, this is my current code. It works perfect in Firefox, but IE only outputs good. IE8 debugger doesn't report any errors either...

function onYouTubePlayerReady(playerId) {
  if (playerId && playerId != 'undefined') {
    if(playerId && playerId == 'ytvideo1'){
      var ytswf = document.getElementById('ytplayer1');
      alert('good');
    } else if(playerId && playerId == 'ytvideo2'){
      var ytswf = document.getElementById('ytplayer2');
    } else {
    }

    setInterval('', 1000);
    ytswf.addEventListener('onStateChange', 'onytplayerStateChange');
    alert('great');
  }
}


function onytplayerStateChange(newState) {
  alert('amazing');
  if(newState == 0){
    $.fn.colorbox.close();
    alert('perfect');
  }
}

更新 3:解决方案

只需将 onComplete 放入我的颜色框中,然后将 swfobject 放入其中即可在 IE 中完美运行.

Simply put onComplete in my colorbox and put the swfobject in that and it worked perfectly in IE.

推荐答案

在 IE 中测试看起来像你正在使用的参考

from testing in IE it looks like the reference you are using

ytswf = document.getElementById('ytplayer1');

在加载实际 swf 对象之前分配,因此 IE 认为您指的是简单的 div 元素

is assigned before the actual swf object is loaded, so IE thinks you are referring to a simple div element

你需要运行这段代码

function onYouTubePlayerReady(playerId) {
  ytswf = document.getElementById("ytplayer1");
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

在你打电话后

swfobject.embedSWF("http://www.youtube.com/v/SPWU-EiulRY?
hl=en_US&hd=0&rel=0&fs=1&autoplay=1&enablejsapi=1&playerapiid=ytvideo1",
"popupVideoContainer1", "853", "505", "8", null, null, params, atts);

在你关闭 $(function()

并放置 var ytswf;<script> 之后而不是进一步向下

and place var ytswf; right after the <script> instead of further down

相关文章