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

后端 未结 5 534
逝去的感伤
逝去的感伤 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:31

    To my knowledge the "preventDefault()" call is about preventing the native browser responses to things like clicks on anchor tags or keypresses in text fields. Once the event handling cycle is over, it's over. For made-up events, I don't think it has any effect at all since it's all about the jQuery event processing system and not about native browser functionality.

    Your code could set some sort of flag somewhere in order to communicate with the "outside world."

    [edit] ooh you could try having the handler stash a reference to the event object somewhere that the exteral code can find it, and then externally check with "isDefaultPrevented()". I don't know whether that'd work however.

提交回复
热议问题