Created
March 1, 2012 02:57
-
-
Save JeffreyWay/1946898 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var addEvent = (function () { | |
var filter = function(el, type, fn) { | |
for ( var i = 0, len = el.length; i < len; i++ ) { | |
addEvent(el[i], type, fn); | |
} | |
}; | |
if ( document.addEventListener ) { | |
return function (el, type, fn) { | |
if ( el && el.nodeName || el === window ) { | |
el.addEventListener(type, fn, false); | |
} else if (el && el.length) { | |
filter(el, type, fn); | |
} | |
}; | |
} | |
return function (el, type, fn) { | |
if ( el && el.nodeName || el === window ) { | |
el.attachEvent('on' + type, function () { return fn.call(el, window.event); }); | |
} else if ( el && el.length ) { | |
filter(el, type, fn); | |
} | |
}; | |
})(); | |
// usage | |
addEvent( document.getElementsByTagName('a'), 'click', fn); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment