addEventListener in Internet Explorer

后端 未结 8 2058
感情败类
感情败类 2020-11-22 14:33

What is the equivalent to the Element Object in Internet Explorer 9?

if (!Element.prototype.addEventListener) {
    Element.prototype.addEventListener = func         


        
相关标签:
8条回答
  • 2020-11-22 14:44

    I would use these polyfill https://github.com/WebReflection/ie8

    <!--[if IE 8]><script
      src="//cdnjs.cloudflare.com/ajax/libs/ie8/0.2.6/ie8.js"
    ></script><![endif]-->
    
    0 讨论(0)
  • 2020-11-22 14:52

    EDIT

    I wrote a snippet that emulate the EventListener interface and the ie8 one, is callable even on plain objects: https://github.com/antcolag/iEventListener/blob/master/iEventListener.js

    OLD ANSWER

    this is a way for emulate addEventListener or attachEvent on browsers that don't support one of those
    hope will help

    (function (w,d) {  // 
        var
            nc  = "", nu    = "", nr    = "", t,
            a   = "addEventListener",
            n   = a in w,
            c   = (nc = "Event")+(n?(nc+= "", "Listener") : (nc+="Listener","") ),
            u   = n?(nu = "attach", "add"):(nu = "add","attach"),
            r   = n?(nr = "detach","remove"):(nr = "remove","detach")
    /*
     * the evtf function, when invoked, return "attach" or "detach" "Event" functions if we are on a new browser, otherwise add "add" or "remove" "EventListener"
     */
        function evtf(whoe){return function(evnt,func,capt){return this[whoe]((n?((t = evnt.split("on"))[1] || t[0]) : ("on"+evnt)),func, (!n && capt? (whoe.indexOf("detach") < 0 ? this.setCapture() : this.removeCapture() ) : capt  ))}}
        w[nu + nc] = Element.prototype[nu + nc] = document[nu + nc] = evtf(u+c) // (add | attach)Event[Listener]
        w[nr + nc] = Element.prototype[nr + nc] = document[nr + nc] = evtf(r+c) // (remove | detach)Event[Listener]
    
    })(window, document)
    
    0 讨论(0)
  • 2020-11-22 14:55

    addEventListener is supported from version 9 onwards; for older versions use the somewhat similar attachEvent function.

    0 讨论(0)
  • 2020-11-22 14:56

    I'm using this solution and works in IE8 or greater.

    if (typeof Element.prototype.addEventListener === 'undefined') {
        Element.prototype.addEventListener = function (e, callback) {
          e = 'on' + e;
          return this.attachEvent(e, callback);
        };
      }
    

    And then:

    <button class="click-me">Say Hello</button>
    
    <script>
      document.querySelectorAll('.click-me')[0].addEventListener('click', function () {
        console.log('Hello');
      });
    </script>
    

    This will work both IE8 and Chrome, Firefox, etc.

    0 讨论(0)
  • 2020-11-22 14:58

    addEventListener is the proper DOM method to use for attaching event handlers.

    Internet Explorer (up to version 8) used an alternate attachEvent method.

    Internet Explorer 9 supports the proper addEventListener method.

    The following should be an attempt to write a cross-browser addEvent function.

    function addEvent(evnt, elem, func) {
       if (elem.addEventListener)  // W3C DOM
          elem.addEventListener(evnt,func,false);
       else if (elem.attachEvent) { // IE DOM
          elem.attachEvent("on"+evnt, func);
       }
       else { // No much to do
          elem["on"+evnt] = func;
       }
    }
    
    0 讨论(0)
  • 2020-11-22 14:59

    Here's something for those who like beautiful code.

    function addEventListener(obj,evt,func){
        if ('addEventListener' in window){
            obj.addEventListener(evt,func, false);
        } else if ('attachEvent' in window){//IE
            obj.attachEvent('on'+evt,func);
        }
    }
    

    Shamelessly stolen from Iframe-Resizer.

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