Javascript addEventListener onStateChange 在 IE 中不起作用
我有两个 colorbox 弹出框,每个框都显示一个 YouTube 视频。当他们玩完后,我试图让他们自动关闭彩盒窗口。下面的代码在 Firefox 中完美运行,但在 IE 中我无法让 addEventListener
工作。我尝试了 attachEvent
但没有成功。有人可以就如何解决这个问题提供任何建议吗?这看起来很简单,但我已经筋疲力尽地试图找到解决方案。
更新 1:
嗯,这是我当前的代码。它在 Firefox 中运行完美,但 IE 只能输出良好。 IE8 调试器也不报告任何错误...
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 放入我的 colorbox 中,然后将 swfobject 放入其中,它就可以在 IE 中完美运行。
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.
UPDATE 1:
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');
}
}
Update 3: Solution
Simply put onComplete in my colorbox and put the swfobject in that and it worked perfectly in IE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IE 不支持
addEventListener
是吗?您需要attachEvent
对吗?IE doesn't support
addEventListener
does it?? You needattachEvent
right?从 IE 中的测试来看,您使用的引用似乎
是在加载实际 swf 对象之前分配的,因此 IE 认为您引用的是一个简单的 div 元素,
您需要
在调用后立即
运行此代码,然后再关闭该
$(function()
并放置
var ytswf;
就在
之后
而不是进一步向下
from testing in IE it looks like the reference you are using
is assigned before the actual swf object is loaded, so IE thinks you are referring to a simple div element
you need to run this code
right after you call
before you close out that
$(function()
and place
var ytswf;
right after the
<script>
instead of further down
新答案
YouTube 播放器对象实现了自己的 addEventListener 方法,该方法更像 AS3 的语法。根据此处列出的信息:
YouTube 在我链接的页面上提供了一个示例,我将在此处提供:
YouTube 还提供了一个示例页面,似乎证明他们的示例代码可以在 IE 中运行。我将在此处链接该示例页面。
现在,这里尝试重写代码的相关部分,使其按照 Google/YouTube 提供的示例工作:
因此,事实证明,这里所犯的错误是由于使用方法名称造成的混乱造成的'添加事件监听器'。在 W3C JavaScript 实现中,第二个参数是一个函数,而在 YouTube 实现中,第二个参数是一个字符串。试一试=)。
New answer
The YouTube player object implements its own addEventListener method which is more like how AS3's syntax. As per the information listed here:
YouTube provides an example on the page I linked which I'll provide here:
YouTube also provides an example page that seems to prove out that their example code works in IE. I'll link that example page here.
Now, here's an attempt at re-writing the pertinent parts of your code to work as per the examples provided by Google/YouTube:
So, it turns out that the mistake being made here arises from the confusion created by the use of the method name 'addEventListener'. In the W3C JavaScript implementation, the second parameter is a function while in the YouTube implementation, the second parameter is a string. Give this a shot =).