How to Fire Personal Event in Javascript

前端 未结 6 1893
闹比i
闹比i 2020-12-13 11:15

I can\'t fire personal events using Javascript in IE. In Firefox work great.

My code is:

var evento; 
if(document.createEventObject)  
{  
   evento         


        
6条回答
  •  死守一世寂寞
    2020-12-13 12:08

    In IE11 document.dispatchEvent still doesn't work, but now attachEvent is missing too, so the other solution is not going to work either. However, I came up with one even uglier. :) It involves replacing the addEventListener method and goes on like this:

    var oldEventListener = document.addEventListener;
    
    document.addEventListener = function (event, func, capture) {
        if (event == "MyPreciousCustomEvent") {
            document.MyPreciousCustomEvent = func;
        }
    
        oldEventListener.call(document, event, func, capture);
    };
    
    ...
    
    $(function () {
        try {
            document.MyPreciousCustomEvent("MyPreciousCustomEvent", {});
        } catch (e) {}
    });
    

    Hope this helps someone.

提交回复
热议问题