Skip to content

Instantly share code, notes, and snippets.

@azu
Created January 29, 2009 13:48
Show Gist options
  • Save azu/54546 to your computer and use it in GitHub Desktop.
Save azu/54546 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>eventLiner</title>
<script language="JavaScript">
<!--
window.onload = function(){
var inputTag = document.getElementById("checkButton");
if (inputTag.addEventListener) {
inputTag.addEventListener("click",miko = function(e){alert("1");},true);
inputTag.addEventListener("click", function(e){alert("2");},true);
inputTag.addEventListener("click", function(e){alert("3");},true);
inputTag.addEventListener("click",function(e){
alert("hoge");
inputTag.removeEventListener("click",arguments.callee,true);
//このリスナーだけ削除できる
} ,true);
//DOMブラウザーのみ
}else{
inputTag.attachEvent("onclick", miko = function(){alert("1");})
inputTag.attachEvent("onclick", function(){alert("2");})
inputTag.attachEvent("onclick", function(){alert("3");})
//IEのみ、イベントキャプチャーがない
}
var clrTag = document.getElementById("clearButton");
if(clrTag.addEventListener){
clrTag.addEventListener("click",function(){
inputTag.removeEventListener("click",arguments.callee,true);
//clearButtonを押したらinputTagのイベントリスナーを全て解除したい
//無名関数を上手く指定できない
},true);
}else{//IE
clrTag.attachEvent("onclick",function(){
inputTag.detachEvent("click",arguments.callee,true);
//clearButtonを押したらinputTagのイベントリスナーを全て解除したい
});
}
}
// -->
</script>
</head>
<body>
<form action="./" method="get">
<input type="button" id="checkButton" value="イベントのチェック">
</form>
<p>IEはaddEventListnerではなく、attachEventとなっている。
その他のブラウザーはaddEventListner(イベント名,イベントハンドラ,イベントキャプチャー)になってる。
</p>
<form action="./" method="get">
<input type="button" id="clearButton" value="イベントの解除">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment