js添加事件、移除事件、阻止冒泡、阻止浏览器默认行为等写法(兼容IE/FF/CHROME)
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),古老的榕树,5-wow.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。