[Add event mechanism] addEventListener and attachEvent
[W3C]
addEventListener('click' , function(){alert('Hello World')} ,false ) //W3C specification adds events (IE8 and above are incompatible); the first parameter is the event type, the second is the event program, the third false is the event bubble, and true is event capture
[IE]
attachEvent('onclick',function(){alert('Hello World')}) //IE adds an event; the first parameter is the event type (need to add on); the second is the event program; because IE only supports event bubbles, there are only two parameters
[Cross-browser compatibility]
function insertEvent(obj,Event,fun){ if(obj.addEvenListener){ addEventLitener(Event,fun,false) }else if(obj.attach){ attachEvent('on'+Event,fun) } }[Delete Event Mechanism] removeEventListener detachEvent
[W3C] removeEventListener
removeEventListener() // Events added using addEventListener can only be deleted through removeEventListener;
[Note] //The following method of deleting events is wrong, because the event program must not be an anonymous function
addEventListener('click' , function(){alert('Hello World')} ,false )
removeEventListener('click',function(){alert('Hello World')}, false)
[solve]
addEventListener('click' , box ,false );removeEventListener('click',box,false) ;function box(){ alert('Hello World');}[Cross-browser compatibility]
function deleteEvent(obj,Event,fun){ if(obj.removeEventListener){ removeEventListener(Event,fun,false); }else if(obj.detachEvent){ detachEvent('on'+Event,fun) }}【Default behavior for blocking specific events】
[preventDefault and returnValue]
[W3C] preventDefault
[IE] reutrnValue
[Cross-browser compatibility]
function(event){ event=event || window.event; if(event.preventDefault){ event.preventDefault() }else{ event.returnValue=false; }}【Get the target object】
[target and srcElement]
function(event){ event=event || window.event; if(event.target){ return event.target; }else if(event.srcElement){ return event.srcElement; } }The above javaScript event mechanism is compatible with [detailed compilation]. It is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.