純粋なJavaScriptは各ブラウザの追加と削除に対応しています.
2202 ワード
// , , , ,
// , ,dom2 ie , , dom0
var eventUtil ={
addEvent:function(element,type,handler){
if (element.addEventListener) {
// IE dom2 ,type :click,handler ,false , true
// netbeans ,
// IE :addEventListener
element.addEventListener(type,handler,false);
}else if (element.attachEvent) {
// IE , attachEvent
element.attachEvent('on'+type,handler);
}else{
element['on'+type] = handler;
}
},
removeEvent:function(element,type,handler){
if (element.removeEventListener) {
// IE dom2 ,type :click,handler ,false , true
// netbeans ,
// IE :removeEventListener
element.removeEventListener(type,handler,false);
}else if (element.detachEvent) {
// IE , detachEvent
element.detachEvent('on'+type,handler);
}else{
//dom0 , null
element['on'+type] = null;
}
},
getEvent:function(event){
//
return event?event:window.event;
},
getType:function(event){
//
return event.type;
},
getElement:function(event){
//
return event.target || event.srcElement;
},
preventDefault:function(event){
//
if(event.preventDefault){
event.preventDefault();
}else{
event.returnValue = false;
}
},
stopProPagation:function(event){
//
if(event.stopProPagation){
event.stopProPagation();
}else{
event.cancelBubble = true;
}
}
}