jQuery: How do I use event.preventDefault() with custom events?

后端 未结 5 533
逝去的感伤
逝去的感伤 2021-02-01 02:48

How can I know in my triggering code that preventDefault has been called?

$(document).trigger(\'customEvent\', params);
if (/* ??? */)
    doDefault         


        
5条回答
  •  悲哀的现实
    2021-02-01 03:26

    trigger() can also take an event object, so if you can create an event object, like so:

    var event = jQuery.Event("customEvent");
    $(document).trigger(event);
    

    then you can check after the trigger to see if preventDefault() has been called like so:

    var prevented = event.isDefaultPrevented();
    

提交回复
热议问题