Internet Explorer 中的 addEventListener [英] addEventListener in Internet Explorer
问题描述
与 Internet Explorer 9 中的元素对象等价的是什么?
What is the equivalent to the Element Object in Internet Explorer 9?
if (!Element.prototype.addEventListener) {
Element.prototype.addEventListener = function() { .. }
}
它在 Internet Explorer 中是如何工作的?
How does it works in Internet Explorer?
如果有一个函数等于 addEventListener
而我不知道,请解释.
If there's a function equal to addEventListener
and I don't know, explain please.
任何帮助将不胜感激.随意提出一种完全不同的解决问题的方法.
Any help would be appreciated. Feel free to suggest a completely different way of solving the problem.
推荐答案
addEventListener
是用于附加事件处理程序的正确 DOM 方法.
addEventListener
is the proper DOM method to use for attaching event handlers.
Internet Explorer(最高版本 8)使用了替代的 attachEvent
方法.
Internet Explorer (up to version 8) used an alternate attachEvent
method.
Internet Explorer 9 支持正确的 addEventListener
方法.
Internet Explorer 9 supports the proper addEventListener
method.
下面应该是尝试编写一个跨浏览器的addEvent
函数.
The following should be an attempt to write a cross-browser addEvent
function.
function addEvent(evnt, elem, func) {
if (elem.addEventListener) // W3C DOM
elem.addEventListener(evnt,func,false);
else if (elem.attachEvent) { // IE DOM
elem.attachEvent("on"+evnt, func);
}
else { // No much to do
elem["on"+evnt] = func;
}
}
这篇关于Internet Explorer 中的 addEventListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!