event.stopPropagation()

event.stopPropagation()返回类型:

描述:防止事件沿着DOM树向上传播,防止任何父处理函数被事件通知到。

We can use event.isPropagationStopped() to determine if this method was ever called (on that event object).

This method works for custom events triggered with trigger() as well.

Note that this will not prevent other handlers on the same element from running.

补充说明:

  • Since the .live() method handles events once they have propagated to the top of the document, it is not possible to stop propagation of live events. Similarly, events handled by .delegate() will propagate to the elements to which they are delegated; event handlers bound on any elements below it in the DOM tree will already have been executed by the time the delegated event handler is called. These handlers, therefore, may prevent the delegated handler from triggering by calling event.stopPropagation() or returning false.

示例:

Kill the bubbling on the click event.

1
2
3
4
$( "p" ).click(function( event ) {
event.stopPropagation();
// Do something
});