How to prevent other event handlers, from first handler, in jQuery

后端 未结 2 1209
深忆病人
深忆病人 2020-12-06 08:59

If you attach two or more event handlers to an HTML element, they would be executed one after the other. However, if you want to stop the subsequent event handlers, based on

相关标签:
2条回答
  • 2020-12-06 09:42

    See event.stopImmediatePropagation() - http://api.jquery.com/event.stopImmediatePropagation/

    According to the jQuery API docs, this method:

    Keeps the rest of the handlers from being executed and prevents the event from bubbling up the DOM tree.

    0 讨论(0)
  • 2020-12-06 09:48

    The solution is browser specific. See dojo.stopEvent:

    dojo.stopEvent = function(/*Event*/ evt){
        // summary:
        //        prevents propagation and clobbers the default action of the
        //        passed event
        // evt: Event
        //        The event object. If omitted, window.event is used on IE.
        if(has("dom-addeventlistener") || (evt && evt.preventDefault)){
            evt.preventDefault();
            evt.stopPropagation();
        }else{
            evt = evt || window.event;
            evt.cancelBubble = true;
            on._preventDefault.call(evt);
        }
    };
    

    Updated fiddle: http://jsfiddle.net/pFp7P/1/

    0 讨论(0)
提交回复
热议问题