Cancel jQuery event handling

后端 未结 4 1680
萌比男神i
萌比男神i 2021-02-14 12:36

I have setup onclick event handler in the following manner:

element.onclick = function() { /*code */ }

Imagine there are event handlers setup u

4条回答
  •  孤独总比滥情好
    2021-02-14 13:15

    I'm not 100% sure what you're asking but maybe this will help:

    You can create a new event object (compliant with W3C DOM) via jQuery's exposed Event constructor:

    For example:

    element.onclick = function(e) {
        var aBetterEventObject = jQuery.Event(e);
        // Now you can do what you want: (Cross-browser)
        aBetterEventObject.preventDefault()
        aBetterEventObject.isDefaultPrevented()
        aBetterEventObject.stopPropagation()
        aBetterEventObject.isPropagationStopped()
        aBetterEventObject.stopImmediatePropagation()
        aBetterEventObject.isImmediatePropagationStopped()
    }
    

    EDIT: Reading through your question again, I don't think propagation is the problem - you seem to want to cancel an event handler from running within an event handler - I'm not sure this is possible. You could just unbind all handlers (jQuery(elem).unbind('click')) but I don't think that's what you're after...

提交回复
热议问题