addEventListener not working in IE8

前端 未结 9 1548
耶瑟儿~
耶瑟儿~ 2020-11-22 15:40

I have created a checkbox dynamically. I have used addEventListener to call a function on click of the checkbox, which works in Google Chrome and Firefox but <

9条回答
  •  轮回少年
    2020-11-22 16:04

    I've opted for a quick Polyfill based on the above answers:

    //# Polyfill
    window.addEventListener = window.addEventListener || function (e, f) { window.attachEvent('on' + e, f); };
    
    //# Standard usage
    window.addEventListener("message", function(){ /*...*/ }, false);
    

    Of course, like the answers above this doesn't ensure that window.attachEvent exists, which may or may not be an issue.

提交回复
热议问题