AddEventListener按规范不返回值,也不会返回值。如果AddEventListener要返回一个值,那么它将是无用的,因为触发回调函数的事件将获取返回值,而不是仅注册它的AddEventListener。
addEventListener( 'onload', function() {
// do something
return true;
// bool(true) would be returned to window.event[onload] and not to addEventListener if that were the case which would still make it useless to you.
}, false );
这么说,有一个肮脏的方法,但它应该得到你想要的。
var eventTracker = {
retVal: false,
retEvt: false,
trigger: function( e ) {
e = e || window.event;
// some code here
}
};
function someFn(e) {
e = e || window.event;
// Some code here
eventTracker.retVal = true;
eventTracker.retEvt = e.type;
eventTracker.trigger.call( e );
}
// Bind the event in all browsers
if ( window.addEventListener ) {
window.addEventListener( 'load', someFn, false );
} else if ( window.attachEvent ) {
window.attachEvent( 'onload', someFn );
} else {
window.onload = someFn;
}