模式-初始化分支

var MYAPP = {};
MYAPP.event = {
    addListener: null,
    removeListener: null
};

if (typeof window.addEventListener === 'function') {
    MYAPP.event.addListener = function(el, type, fn) {
        el.addEventListener(type, fn, false);
    };
    MYAPP.event.removeListener = function(el, type, fn) {
        el.removeListener(type, fn, false);
    }
} else if (typeof window.attachEvent === 'function') {
    MYAPP.event.addListener = function(el, type, fn) {
        el.attachEvent('on' + type, fn);
    };
    MYAPP.event.removeListener = function(el, type, fn) {
        el.detachEvent('on' + type, fn);
    };
} else {
    MYAPP.event.addListener = function(el, type, fn) {
        el['on' + type] = fn;
    };
    MYAPP.event.removeListener = function(el, type, fn) {
        el['on' + type] = null;
    };
};

posted @ 2017-04-06 17:41  佛陀  阅读(196)  评论(0编辑  收藏  举报