并非所有版本的 IE 都将事件作为参数传递。它在window.event
。您可以将此添加到您的 _addEvent shim 或事件处理程序中:
e = e || window.event
如:
_addEvent(eventableIframes[i], 'mousemove', function (e) {
e = e || window.event;
utilBarHandler(e);
});
这将适用于 IE 或其他。
这是跨浏览器事件处理的两种风格,可确保在 IE 中正确设置event
和指针:this
// add event cross browser
function addEvent(elem, event, fn) {
if (elem.addEventListener) {
elem.addEventListener(event, fn, false);
} else {
elem.attachEvent("on" + event, function() {
// set the this pointer same as addEventListener when fn is called
return(fn.call(elem, window.event));
});
}
}
// refined add event cross browser
function addEvent(elem, event, fn) {
// avoid memory overhead of new anonymous functions for every event handler that's installed
// by using local functions
function listenHandler(e) {
var ret = fn.apply(this, arguments);
if (ret === false) {
e.stopPropagation();
e.preventDefault();
}
return(ret);
}
function attachHandler() {
// set the this pointer same as addEventListener when fn is called
// and make sure the event is passed to the fn also so that works the same too
var ret = fn.call(elem, window.event);
if (ret === false) {
window.event.returnValue = false;
window.event.cancelBubble = true;
}
return(ret);
}
if (elem.addEventListener) {
elem.addEventListener(event, listenHandler, false);
} else {
elem.attachEvent("on" + event, attachHandler);
}
}