Created
February 29, 2012 05:03
-
-
Save honbin/1937994 to your computer and use it in GitHub Desktop.
イベントハンドラの共通化
This file contains hidden or 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
// htmlがこんな感じだとして | |
// <div id='hoge'>hoge</div> | |
// | |
var addEvent = function(elName, ev, fn) { | |
var el = document.getElementById(elName); | |
if(el.addEventListener) { | |
el.addEventListener(ev, fn, false); | |
} else if(el.attachEvent){ | |
el.attachEvent('on' + ev, fn); | |
} else { | |
el['on' + ev] = fn; | |
} | |
} | |
new addEvent('hoge', 'click', function(){ window.alert('click!!') } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment