How to trigger event in JavaScript?

前端 未结 18 2257
情深已故
情深已故 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

    if you use jQuery, you can simple do

    $('#yourElement').trigger('customEventName', [arg0, arg1, ..., argN]);
    

    and handle it with

    $('#yourElement').on('customEventName',
       function (objectEvent, [arg0, arg1, ..., argN]){
           alert ("customEventName");
    });
    

    where "[arg0, arg1, ..., argN]" means that these args are optional.

提交回复
热议问题