Trigger event with parameters

前端 未结 3 1164
鱼传尺愫
鱼传尺愫 2021-02-04 03:09

This is pretty annoying. I want to just trigger an event in javascript. I need to pass the event object into the parameters as usual and an additional custom parameter.

3条回答
  •  情歌与酒
    2021-02-04 03:19

    the event object and an additional custom parameter

    That's impossible with the native DOM methods. Handlers are called with only one argument, which is the event object. So if there is any data you need to pass along, you need to make it a (custom) property of your event object. You might use the DOM4 CustomEvent constructor for that (see the MDN docs for a cross-browser shim).

    Then use dispatchEvent as you would normally with (custom or native) script-triggered events. For IE below 9, you seem to need to use fireEvent.

提交回复
热议问题