event.stopPropagation()和之间有什么区别event.stopImmediatePropagation()?
event.stopPropagation()
event.stopImmediatePropagation()
stopPropagation将防止任何 父 被执行的处理程序stopImmediatePropagation将防止任何父处理程序 ,并且还 任何 其他 处理程序从执行
stopPropagation
stopImmediatePropagation
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>
请注意,事件绑定的顺序在这里很重要!
$("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>