How to simulate a mouse click using JavaScript?

后端 未结 7 1076
无人及你
无人及你 2020-11-22 00:57

I know about the document.form.button.click() method. However, I\'d like to know how to simulate the onclick event.

I found this code somew

7条回答
  •  你的背包
    2020-11-22 01:52

    Based on Derek's answer, I verified that

    document.getElementById('testTarget')
      .dispatchEvent(new MouseEvent('click', {shiftKey: true}))
    

    works as expected even with key modifiers. And this is not a deprecated API, as far as I can see. You can verify on this page as well.

提交回复
热议问题