Object doesn't support property or method 'attachEvent' InternetExplorer 11

后端 未结 2 496
滥情空心
滥情空心 2021-01-06 07:16

If I press the form send button \"Saada\" in IE11, I will get an error:

Object doesn\'t support property or method \'attachEvent\'

相关标签:
2条回答
  • 2021-01-06 07:34

    The javascript method attachEvent was replaced with the method addEventListener in IE11.

    JQuery 1.10.1 still uses this method in case of IE > 8. This will cause javascript compilation errors.

    JQuery 1.10.2 seems to have solved this problem.

    Hope this will help

    0 讨论(0)
  • 2021-01-06 07:46

    attachEvent is a deprecated function used in older versions of Internet Explorer. For modern browsers use this instead.

    el.addEventListener(evt,func,false);
    

    See documentation here

    You could also create a function which checks which function to use

    function addListener(el, event, func){
        if (el.addEventListener) {
           el.addEventListener(event, func, false);
        }
        else {
           el.attachEvent("on"+event, func);
        }
    }
    

    Then you can attach your event by doing this:

    var element = document.getElementById('myElement');
    addListener(element, 'click', function(){
        alert('You have clicked!');
    });
    

    If you are unable to to this then perhaps a polyfill will work instead. Try to insert this somewhere:

    if(!document.attachEvent){
      Object.prototype.attachEvent=function(event,func){
        this.addEventListener(event.split('on')[1], func);
      }
    }
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题