addEventListener in Internet Explorer

后端 未结 8 2065
感情败类
感情败类 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: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;
       }
    }
    

提交回复
热议问题