日期:2014-05-16 浏览次数:20369 次
var Event = {}; //方法一 runtime Event.addEventListener = function(obj, eventType, listener){ if(typeof window.addEventListener === 'function') {//DOM2接口 el.addEventListener(type, fn, false); } else if(typeof document.attachEvent === 'function') {//IE el.attachEvent('on' + type, fn); } else {//DOM0接口 el['on' + type] = fn; } };
//方法二 initialize if(typeof window.addEventListener === 'function') {//DOM2接口 Event.addEventListener = function(el, type, fn) { el.addEventListener(type, fn, false); }; } else if(typeof document.attachEvent === 'function') {//IE Event.addEventListener = function(el, type, fn) { el.attachEvent('on' + type, fn); }; } else {//DOM0接口 Event.addEventListener = function(el, type, fn) { el['on' + type] = fn; }; }
//方法三 lazy mode Event.addEventListener = function(obj, eventType, listener){ if (!Event.addEventListener) { if(typeof window.addEventListener === 'function') {//DOM2接口 Event.addEventListener = function(el, type, fn) { el.addEventListener(type, fn, false); }; } else if(typeof document.attachEvent === 'function') {//IE Event.addEventListener = function(el, type, fn) { el.attachEvent('on' + type, fn); }; } else {//DOM0接口 Event.addEventListener = function(el, type, fn) { el['on' + type] = fn; }; } } Event.addEventListener(obj, eventType, listener); }