Clone a DOM event object to re-dispatch

后端 未结 3 1334
青春惊慌失措
青春惊慌失措 2021-01-04 11:13

Some browsers won\'t allow you to re-dispatch an event that has already been dispatched, but allow you to create new event objects based on values that can be obtained from

3条回答
  •  攒了一身酷
    2021-01-04 11:25

    I found my own answer, at least for MouseEvents specifically:

    function cloneMouseEvent( e ) {
        var evt = document.createEvent( "MouseEvent" );
        evt.initMouseEvent( e.type, e.canBubble, e.cancelable, e.view, e.detail, e.screenX, e.screenY, e.clientX, e.clientY, e.ctrlKey, e.altKey, e.shiftKey, e.metaKey, e.button, e.relatedTarget );
        return evt;
    }
    

    You can then dispatch the event on a target with:

    target.dispatchEvent( evt );
    

提交回复
热议问题