stopPropagation
will prevent any parent handlers from being executed stopImmediatePropagation
will prevent any parent handlers and also any other handlers from executing
(stopPropagation
将阻止执行任何父处理程序stopImmediatePropagation
将阻止任何父处理程序以及任何其他处理程序执行)
Quick example from the jquery documentation:
(jquery文档中的快速示例:)
$("p").click(function(event) { event.stopImmediatePropagation(); }); $("p").click(function(event) { // This function won't be executed $(this).css("background-color", "#f00"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>example</p>
Note that the order of the event binding is important here!
(请注意,事件绑定的顺序在这里很重要!)
$("p").click(function(event) { // This function will now trigger $(this).css("background-color", "#f00"); }); $("p").click(function(event) { event.stopImmediatePropagation(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>example</p>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…