addEventListener
is the proper DOM method to use for attaching event handlers.
Internet Explorer (up to version 8) used an alternate attachEvent
method.
Internet Explorer 9 supports the proper addEventListener
method.
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;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…