How to dispatch a double click event on an element?

前端 未结 3 1374
情深已故
情深已故 2021-01-13 01:04

I can dispatch a simple click event (without any optional parameters) with:

var clickEvt = document.createEvent(\"MouseEvents\");
clickEvt.initEvent(\"click\         


        
相关标签:
3条回答
  • 2021-01-13 01:38

    The feauture used in the accepted answer (Event.initEvent()) has been deprecated.

    Instead, use specific event consructors like Event()

    So to answer your question, it would be something like :

    var doubleClickEvent = new Event('dblclick');      //define double click event
    
    element.dispatchEvent(doubleClickEvent);           //trigger double click even
    
    0 讨论(0)
  • 2021-01-13 01:42

    This worked in Chrome@83

    const event = new MouseEvent('dblclick', {
      bubbles: true,
      cancelable: true,
      view: window
    });
    
    const buttonEl = document.querySelector('#my-button');
    buttonEl.dispatchEvent(event);
    
    0 讨论(0)
  • 2021-01-13 01:56

    Just substitute "click" with "dblclick" in clickEvt.initEvent:

    var dblclickEvt = document.createEvent("MouseEvents");
    dblclickEvt.initEvent("dblclick");
    element.dispatchEvent(dblclickEvt);
    
    0 讨论(0)
提交回复
热议问题