How to programmatically fire a dblclick event defined with addEventListener?

前端 未结 2 1804
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 15:37

For JS Unit test, I need to check that a double-click behaves as expected. The issue is that the event was registered via element.addEventListener. And for some reason, in t

2条回答
  •  抹茶落季
    2020-12-16 16:03

    You can use dispatchEvent to programatically trigger events:

    var event = new MouseEvent('dblclick', {
        'view': window,
        'bubbles': true,
        'cancelable': true
      });
    document.getElementById('aa').dispatchEvent(event);
    

    See the section "Triggering built-in events" on MDN.

    Here is a fiddle of the code in action.

提交回复
热议问题