javascript add event remove event_javascript技巧
程序员文章站
2022-05-22 20:54:25
...
网上搜来的,看样子不错,记一笔。//------------------------------------
// heavily based on the Quirksmode addEvent contest winner, John Resig
// addEvent
function addEvent(obj,type,fn){
if(obj.addEventListener) obj.addEventListener(type,fn,false);
else if(obj.attachEvent){
obj["e"+type+fn]=fn;
obj[type+fn]=function(){obj["e"+type+fn](window.event);}
obj.attachEvent("on"+type,obj[type+fn]);
}
}
//------------------------------------
// removeEvent
function removeEvent(obj,type,fn){
if(obj.removeEventListener) obj.removeEventListener(type,fn,false);
else if(obj.detachEvent){
obj.detachEvent("on"+type,obj[type+fn]);
obj[type+fn]=null;
obj["e"+type+fn]=null;
}
}
// heavily based on the Quirksmode addEvent contest winner, John Resig
// addEvent
function addEvent(obj,type,fn){
if(obj.addEventListener) obj.addEventListener(type,fn,false);
else if(obj.attachEvent){
obj["e"+type+fn]=fn;
obj[type+fn]=function(){obj["e"+type+fn](window.event);}
obj.attachEvent("on"+type,obj[type+fn]);
}
}
//------------------------------------
// removeEvent
function removeEvent(obj,type,fn){
if(obj.removeEventListener) obj.removeEventListener(type,fn,false);
else if(obj.detachEvent){
obj.detachEvent("on"+type,obj[type+fn]);
obj[type+fn]=null;
obj["e"+type+fn]=null;
}
}
推荐阅读
-
Js event事件在IE、FF兼容性问题_javascript技巧
-
Add a Picture to a Microsoft Word Document_javascript技巧
-
JavaScript Event学习第八章 事件的顺序_javascript技巧
-
excel操作之Add Data to a Spreadsheet Cell_javascript技巧
-
Add a Picture to a Microsoft Word Document_javascript技巧
-
关于火狐(firefox)及ie下event获取的两种方法_javascript技巧
-
JavaScript Event学习第七章 事件属性_javascript技巧
-
Js event事件在IE、FF兼容性问题_javascript技巧
-
JavaScript Event学习第八章 事件的顺序_javascript技巧
-
自己的js工具 Event封装_javascript技巧