How do I explicitly execute default action from jQuery event

前端 未结 4 1637
一整个雨季
一整个雨季 2020-11-30 13:51

Following up from this question, I\'m trying to implement an unobtrusive confirm dialog.

$(document).ready(function () {
    $(\"[data-confirmPrompt]\").clic         


        
4条回答
  •  难免孤独
    2020-11-30 14:22

    I was able to achieve this by calling event.stopPropagation() from a more specific context, and ensuring that I don't call event.preventDefault(). While you can't call the default action explicitly, you can set up the conditions so that the default action happens — and do as little or as much else as you wish.

        // Normal event handler
        $("[data-toggle]").click(ev => {
          switchToTab(ev.currentTarget)
          ev.preventDefault()
        })
    
        // Allow default handler in a specific case.
        $("[data-toggle] ul a").click(ev => {
          // Don't bubble the event to the less specific handler, above
          ev.stopPropagation()
          // An incorrect return value will also cancel the default action.
          return true
        })
    

提交回复
热议问题