1 var btn5 = document.getElementById('btn5'); 2 btn5.onclick=function(){ 3 console.log(this.id);//btn5 4 };
1 var btn2 = document.getElementById('btn2'); 2 var handlers = function () { 3 console.log(this.id); 4 }; 5 6 btn2.addEventListener('click',handlers,false); 7 8 btn2.removeEventListener('click',handlers.false);
1 var btn3 = document.getElementById('btn3'); 2 var handlers2=function(){ 3 console.log(this===window);//true,注意attachEvent()添加的事件處理程序運行在全局做用域中; 4 }; 5 btn3.attachEvent('onclick',handlers2);
1 //建立的方法是addHandlers(),removeHandlers(),這兩個方法屬於一個叫EventUtil的對象;可是這個沒有考慮到IE中做用域的問題,不過就添加和移除事件仍是足夠的。 2 3 var EventUtil = { 4 addHandlers: function (element, type, handlers) { 5 if (element.addEventListener) { 6 element.addEventListener(type, handlers, false); 7 } else if (element.attachEvent) { 8 element.attachEvent(on + type, handlers); 9 } else { 10 element['on' + type] = handlers; 11 } 12 }, 13 removeHandlers: function (element, type, handlers) { 14 if (element.removeEventListener) { 15 element.removeEventListener(type, handlers, false); 16 } else if (element.detachEvent) { 17 element.detachEvent(on + type, handlers); 18 } else { 19 element['on' + type] = null; 20 } 21 } 22 };
1 var btn4=document.getElementById('btn4'); 2 var handlers3=function(){ 3 console.log('123') 4 }; 5 EventUtil.addHandlers(btn4,'click',handlers3); 6 //…… 7 EventUtil.removeHandlers(btn4,'click',handlers3);
1 //this,target,currentTarget,this===currentTarget 2 $('#outer').on('click','#center',function(e){ 3 console.log(this.id);//on()中間的參數是個過濾器,至關於將事件綁定到了#center上;此時點擊#center將不會觸發事件 4 console.log(e.target.id); 5 console.log(e.currentTarget.id); 6 }); 7 8 $('#outer').on('click',function(e){ 9 console.log(this.id); 10 console.log(e.target.id); 11 console.log(e.currentTarget.id); 12 }); 13 14 event.stopPropagation()不能簡單說阻止了事件的冒泡,其實也阻止了事件的繼續捕獲,確切的說應該是阻止事件的進一步傳播 15 16 var d1 = document.getElementById('d1'); 17 d1.addEventListener('click', function (evt) { 18 console.log('d1'); 19 evt.stopPropagation(); 20 }, true); 21 var d2 = document.getElementById('d2'); 22 d2.addEventListener('click', function () { 23 console.log('d2'); 24 }, true);
1 var d1 = document.getElementById('d1'); 2 d1.addEventListener('click', function (evt) { 3 console.log('d1'); 4 evt.stopImmediatePropagation(); 5 }, true); 6 d1.addEventListener('click', function (evt) { 7 console.log('d1+1'); 8 }, true); 9 var d2 = document.getElementById('d2'); 10 d2.addEventListener('click', function () { 11 console.log('d2'); 12 }, true);
1 var EventUtil = { 2 addHandler: function (element, type, handler) { 3 if (element.addEventListener) { 4 element.addEventListener(type, handler, false); 5 } else if (element.attachEvent) { 6 element.attachEvent(on + type, handler); 7 } else { 8 element['on' + type] = handler; 9 } 10 }, 11 12 getEvent: function (event) { 13 return event ? event : window.event; 14 15 }, 16 17 getTarget: function (event) { 18 return event.target || event.srcElement; 19 }, 20 21 preventDefault: function (event) { 22 if (event.preventDefault) { 23 event.preventDefault(); 24 } else { 25 event.returnValue = false; 26 } 27 }, 28 29 stopPropagation: function (event) { 30 if (event.stopPropagation) { 31 event.stopPropagation(); 32 } else { 33 event.cancelBubble = true; 34 } 35 }, 36 37 removeHandler: function (element, type, handler) { 38 if (element.removeEventListener) { 39 element.removeEventListener(type, handler, false); 40 } else if (element.detachEvent) { 41 element.detachEvent(on + type, handler); 42 } else { 43 element['on' + type] = null 44 } 45 } 46 47 };
1 var myBtn=document.getElementById('my-btn'); 2 btn.onclick=function(event){ 3 event=EventUtil.getEvent(event); 4 EventUtil.preventDefault(event); 5 };
1 var myLinks=document.getElementById('my-links'); 2 myHandlers=function(event){ 3 event=EventUtil.getEvent(event); 4 var target=EventUtil.getTarget(event); 5 6 switch(target.id){ 7 case 'item-1': 8 location.href='http://www.cnblogs.com/lazychen/'; 9 break; 10 case 'item-2': 11 document.title='event'; 12 break; 13 case 'item-3': 14 console.log('hi'); 15 break; 16 } 17 }; 18 EventUtil.addHandler(myLinks,'click',myHandlers);
ps:文中盜了兩張圖~大大莫怪莫怪瀏覽器