var addEvent = function( obj, type, fn ) { if (obj.addEventListener) obj.addEventListener( type, fn, false ); else if (obj.attachEvent) { obj["e"+type+fn] = fn; obj.attachEvent( "on"+type, function() { obj["e"+type+fn].call(obj, window.event); } ); } };
var removeEvent = function( obj, type, fn ) { if (obj.removeEventListener) obj.removeEventListener( type, fn, false ); else if (obj.detachEvent) { obj.detachEvent( "on" +type, obj["e"+type+fn] ); obj["e"+type+fn] = null; } };
var stopEvent = function(e){ e = e || window.event; if(e.preventDefault) { e.preventDefault(); e.stopPropagation(); }else{ e.returnValue = false; e.cancelBubble = true; } },
var stopPropagation = function(e) { e = e || window.event; if (!+"\v1″) { e.cancelBubble = true; } else { e.stopPropagation(); } }
var preventDefault = function(e) { e = e || window.event; if(e.preventDefault) { e.preventDefault(); }else{ e.returnValue = false; } }
var getEventTarget = function(e){ e = e || window.event; var target = event.srcElement ? event.srcElement : event.target; return target; }
var addPropertyChangeEvent = function (obj,fn) { if(window.ActiveXObject){ obj.onpropertychange = fn; }else{ obj.addEventListener("input",fn,false); } }
js添加事件、移除事件、阻止冒泡、阻止浏览器默认行为等写法(兼容IE/FF/CHROME),布布扣,bubuko.com
js添加事件、移除事件、阻止冒泡、阻止浏览器默认行为等写法(兼容IE/FF/CHROME)
原文地址:http://www.cnblogs.com/kt520/p/3810986.html