Cross browser preventDefault() without jQuery

2019-02-12 12:27发布

I wrote this bind method and am having an issue in my preventDefault() method to work in IE. The callback line never executes. Can someone provide assistance? Thanks!

var preventDefault = function (event) {
    if (window.event) { window.event.returnValue = false; }
    else if (event.preventDefault) { event.preventDefault(); }
    else { event.returnValue = false; }
};

var bindEvent = function (ele, type, cb) {
    if (window.addEventListener) {
        ele.addEventListener(type, cb, false);
    } else if (window.attachEvent) {
        ele.attachEvent('on' + type, function () {
            event.preventDefault = function () {
                preventDefault(event);
            }.call(this);
           cb.call(ele, event);  //this does not execute
        });
    }
};

1条回答
The star\"
2楼-- · 2019-02-12 12:54
// cancel event
function cancelEvent(event) {
   if (event.preventDefault) { 
      event.preventDefault();
   } else {
      event.returnValue = false; 
   }
}
查看更多
登录 后发表回答