javascript addEventListener onStateChange在IE中不起作用 [英] javascript addEventListener onStateChange not working in IE
问题描述
我有两个彩盒弹出框,每个弹出框都显示一个youtube视频。当他们完成播放时,我试图让它们自动关闭彩盒窗口。下面的代码在firefox中运行完美,但在IE中我无法使用addEventListener。我试过attachEvent没有成功。任何人都可以提出如何解决这个问题的建议吗?看起来很简单,但我已经筋疲力尽,试图找到解决方案。顺便说一句,这是我第一次在stackoverflow,这是非常令人印象深刻。
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. By the way, this is my first time at stackoverflow and it's very impressive.
更新1:
嗯,这是我目前的代码。它在FF中运行完美,但IE只能输出良好。 IE8调试器也不报告任何错误...
Well, this is my current code. It works perfect in FF, 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;
脚本>
而不是进一步下降
and place var ytswf;
right after the <script>
instead of further down
这篇关于javascript addEventListener onStateChange在IE中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!