Skip to content

Instantly share code, notes, and snippets.

@awkrail
Last active July 19, 2016 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awkrail/e9a1127369cab82a94ca4e11b6c20ca1 to your computer and use it in GitHub Desktop.
Save awkrail/e9a1127369cab82a94ca4e11b6c20ca1 to your computer and use it in GitHub Desktop.
eventListener
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>LTaichi</title>
</head>
<body>
<script>
function addListener(elem,ev,listener){
if(elem.addEventListener){
elem.addEventListener(ev,listener,false);
}else if(elem.attachEvent){
elem.attachEvent('on'+ev,listener);
}
else{
throw new Error('イベントリスナに未対応');
}
}
addListener(window,'load',listen);
function listen(){
addListener(document.getElementById('btn'),'click',function(){
window.alert('ボタンがクリックされました。');
});
</script>
<input type="button" id="btn" value="表示">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment