event:
1 //得到事件 2 function getEvent(evt){ 3 4 if (evt && typeof(evt) != "undefined") { 5 var e = window.event || arguments.callee.caller.arguments[0]; 6 7 if (typeof(evt) != "undefined") { 8 e = window.event || evt; 9 } 10 11 return e; 12 } else { 13 if(window.event) {return window.event;} 14 15 try { 16 func = getEvent.caller; 17 18 while(func!=null){ 19 var arg0=func.arguments[0]; 20 21 if(arg0){ 22 if((arg0.constructor==Event || arg0.constructor ==MouseEvent 23 || arg0.constructor==KeyboardEvent) 24 ||(typeof(arg0)=="object" && arg0.preventDefault 25 && arg0.stopPropagation)){ 26 return arg0; 27 } 28 } 29 30 func = func.caller; 31 } 32 } catch (e) { 33 return null; 34 } 35 36 return null; 37 } 38 }
target:
1 //获取事件操作对象 2 function getTarget(evt) { 3 //var e = window.event || arguments.callee.caller.arguments[0]; //获取触发事件的源对象 4 var e = getEvent(evt); //获取触发事件的源对象 5 6 if (typeof(e) != "undefined") { 7 try { 8 var target = (typeof(e.srcElement) != "undefined") && e.srcElement ? e.srcElement : e.target; 9 } catch (ex) { 10 target = null; 11 } 12 13 } else { 14 target = null; 15 } 16 17 return target; 18 }
事件冒泡:
//事件冒泡 function preventEvent() { //取消事件冒泡 //var e = arguments.callee.caller.arguments[0] || event; //若省略此句,下面的e改为event,IE运行可以,但是其他浏览器就不兼容 var e = getEvent(); //获取触发事件的源对象 if (e && e.stopPropagation) { // this code is for Mozilla and Opera e.stopPropagation(); } else if (window.event) { // this code is for IE window.event.cancelBubble = true; } }