Dispatch event with data

后端 未结 2 1431
后悔当初
后悔当初 2021-02-01 00:39

I\'d like to dispatch an event that will pass some data to any event listeners that listen on that event.

Considering a function that fires an event:

fun         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 01:15

    Unfortunately CustomEvent not work on IE (before Edge), but you can simply use the normal Event.

    Just consider that also Event is an object, and so you can add all custom attributes you need:

     event.X = "foo";
    

    On this way you can check your custom field on the callback function joned to the event.


    NB. For old IE version you need this polyfill for new Event missed function:

    var event;
    if(typeof(Event) === 'function') {
      event = new Event(EVENT_NAME);
    }else{
      event = document.createEvent('Event');
      event.initEvent(EVENT_NAME, false, false);
    }
    

提交回复
热议问题