How to trigger event in JavaScript?

前端 未结 18 1858
情深已故
情深已故 2020-11-21 04:48

I have attached an event to a text box using addEventListener. It works fine. My problem arose when I wanted to trigger the event programmatically from another

18条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 05:08

    Just to suggest an alternative that does not involve the need to manually invoke a listener event:

    Whatever your event listener does, move it into a function and call that function from the event listener.

    Then, you can also call that function anywhere else that you need to accomplish the same thing that the event does when it fires.

    I find this less "code intensive" and easier to read.

提交回复
热议问题