event.stopPropagation()和event.stopImmediatePropagation()之间有什么区别?
当前回答
在这里,我添加了stopPropagation vs stopImmediatePropagation的jsf示例。 JSFIDDLE
let stopProp = document.getElementById('stopPropagation'); let stopImmediate = document.getElementById('stopImmediatebtn'); let defaultbtn = document.getElementById("defalut-btn"); stopProp.addEventListener("click", function(event){ event.stopPropagation(); console.log('stopPropagation..') }) stopProp.addEventListener("click", function(event){ console.log('AnotherClick') }) stopImmediate.addEventListener("click", function(event){ event.stopImmediatePropagation(); console.log('stopimmediate') }) stopImmediate.addEventListener("click", function(event){ console.log('ImmediateStop Another event wont work') }) defaultbtn.addEventListener("click", function(event){ alert("Default Clik"); }) defaultbtn.addEventListener("click", function(event){ console.log("Second event defined will also work same time...") }) div{ margin: 10px; } <p> The simple example for event.stopPropagation and stopImmediatePropagation? Please open console to view the results and click both button. </p> <div > <button id="stopPropagation"> stopPropagation-Button </button> </div> <div id="grand-div"> <div class="new" id="parent-div"> <button id="stopImmediatebtn"> StopImmediate </button> </div> </div> <div> <button id="defalut-btn"> Normat Button </button> </div>
其他回答
在这里,我添加了stopPropagation vs stopImmediatePropagation的jsf示例。 JSFIDDLE
let stopProp = document.getElementById('stopPropagation'); let stopImmediate = document.getElementById('stopImmediatebtn'); let defaultbtn = document.getElementById("defalut-btn"); stopProp.addEventListener("click", function(event){ event.stopPropagation(); console.log('stopPropagation..') }) stopProp.addEventListener("click", function(event){ console.log('AnotherClick') }) stopImmediate.addEventListener("click", function(event){ event.stopImmediatePropagation(); console.log('stopimmediate') }) stopImmediate.addEventListener("click", function(event){ console.log('ImmediateStop Another event wont work') }) defaultbtn.addEventListener("click", function(event){ alert("Default Clik"); }) defaultbtn.addEventListener("click", function(event){ console.log("Second event defined will also work same time...") }) div{ margin: 10px; } <p> The simple example for event.stopPropagation and stopImmediatePropagation? Please open console to view the results and click both button. </p> <div > <button id="stopPropagation"> stopPropagation-Button </button> </div> <div id="grand-div"> <div class="new" id="parent-div"> <button id="stopImmediatebtn"> StopImmediate </button> </div> </div> <div> <button id="defalut-btn"> Normat Button </button> </div>
令人惊讶的是,所有其他答案都只说了一半事实或实际上是错误的!
e.s stopimmediatepropagation()停止为该事件调用任何进一步的处理程序,没有异常 e.s stoppropagation()类似,但如果尚未调用,仍然调用此元素上此阶段的所有处理程序
什么阶段?
例如,一个点击事件总是首先沿着DOM向下走(称为“捕获阶段”),最后到达事件的起源(“目标阶段”),然后再次冒泡(“冒泡阶段”)。使用addEventListener(),您可以分别为捕获阶段和冒泡阶段注册多个处理程序。(目标阶段在目标上调用这两种类型的处理程序,没有区别。)
这是其他答案的错误之处:
quote: “event.stopPropagation() allows other handlers on the same element to be executed” correction: if stopped in the capture phase, bubble phase handlers will never be reached, also skipping them on the same element quote: “event.stopPropagation() [...] is used to stop executions of its corresponding parent handler only” correction: if propagation is stopped in the capture phase, handlers on any children, including the target aren’t called either, not only parents ...and: if propagation is stopped in the bubble phase, all capture phase handlers have already been called, including those on parents
一个小提琴和mozilla.org事件阶段解释与演示。
下面是一个演示来说明两者的区别:
document.querySelectorAll(“按钮”)[0]。addEventListener(“点击”,e = > { e.stopPropagation (); 提醒(1); }); document.querySelectorAll(“按钮”)[1]。addEventListener(“点击”,e = > { e.stopImmediatePropagation (); 提醒(1); }); document.querySelectorAll(“按钮”)[0]。addEventListener(“点击”,e = > { 提醒(2); }); document.querySelectorAll(“按钮”)[1]。addEventListener(“点击”,e = > { 提醒(2); }); < div onclick = "警报(3)" > < >按钮1……2 > < /按钮 <按钮> 1 > < /按钮 < / div >
请注意,可以将多个事件处理程序附加到元素上的事件。
event. stoppropagation()允许执行同一元素上的其他处理程序,而event. stopimmediatepropagation()阻止运行每个事件。例如下面的jQuery代码块。
$("p").click(function(event)
{ event.stopImmediatePropagation();
});
$("p").click(function(event)
{ // This function won't be executed
$(this).css("color", "#fff7e3");
});
如果事件。stopPropagation在前面的例子中使用,然后在p元素上的下一个点击事件改变css将触发,但在case event. stopimmediatepropagation()中,下一个p点击事件将不会触发。
1) event.stopPropagation (): 仅用于停止相应父处理程序的执行。
2) event.stopImmediatePropagation (): 它用于停止其相应的父处理程序的执行,也用于停止除当前处理程序外附加到自身的处理程序或函数的执行。 它还停止所有附加到整个DOM的当前元素的处理程序。
下面是示例:Jsfiddle!
谢谢, sahil聊