自定义 绑定响应函数 解除响应函数 .addEventListener 兼容 .attachEvent

嗯哼。不多说,直接上代码。

  • // 自定义 绑定响应函数    兼容性封装 Test Already.
    function bindEventFunc(obj, eventStr, func){
        // console.log(!!obj.addEventListener == true);    // true
        // console.log(!!obj.attachEvent == true );    // false
        if(obj.addEventListener){
            // 大多数浏览器支持, IE8 及以下不支持
            obj.addEventListener(eventStr, func, false);    // false 指定在捕获的阶段的时候不触发事件
        }else{
            // IE5 - IE10 支持
            obj.attachEvent("on"+eventStr, function(){
                func().call(obj);
            });
        }
    }
  • // 自定义 解除响应函数    兼容性封装 Test Already.
    function removeEventFunc(obj, eventStr, func){
        // console.log(!!obj.removeEventListener == true);    // true
        // console.log(!!obj.detachEvent == true );    // false
        if(obj.removeEventListener){
            // 大多数浏览器支持, IE8 及以下不支持
            obj.removeEventListener(eventStr, func);
        }else if(obj.detachEvent){
            // IE5 - IE10 支持
            obj.detachEvent("on"+eventStr, func);
        }
    }
--------小尾巴 ________一个人欣赏-最后一朵颜色的消逝-忠诚于我的是·一颗叫做野的心.决不受人奴役.怒火中生的那一刻·终将结束...
原文地址:https://www.cnblogs.com/tianxiaxuange/p/9873898.html