注:TargetObj 目标对象 , EventType 事件类型 ,FunctionName 触发事件后的函数
!document.all ? TargetObj.addEventListener(EventType, FunctionName, false) : TargetObj.attachEvent("on" + EventType, FunctionName);
}
function DelEvent(TargetObj, EventType, FunctionName){
!document.all ? TargetObj.removeEventListener(EventType, FunctionName, false) : TargetObj.detachEvent("on" + EventType, FunctionName);
}
例子:
<script type="text/javascript">
window.onload = function(){
var aa=document.getElementById("aa");
AddEvent(aa,"click",clickfun);
}
function clickfun(){
alert("abc");
}
function AddEvent(TargetObj, EventType, FunctionName){
!document.all ? TargetObj.addEventListener(EventType, FunctionName, false) : TargetObj.attachEvent("on" + EventType, FunctionName);
}
function DelEvent(TargetObj, EventType, FunctionName){
!document.all ? TargetObj.removeEventListener(EventType, FunctionName, false) : TargetObj.detachEvent("on" + EventType, FunctionName);
}
function clearfun(){
var aa = document.getElementById("aa");
DelEvent(aa,"click",clickfun);
}
</script>
<body>
<input type="button" value="event" id="aa">
<input type="button" value="clear" onclick="clearfun()">
</body>
注意:只有是用AddEvent方法添加上去的事件才能够 delete.
像<input type="button" value="clear" onclick="clearfun()"> 中的 onclick事件是不能删除的。。。