Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

javascript - stopPropagation与stopImmediatePropagation(stopPropagation vs. stopImmediatePropagation)

event.stopPropagation()event.stopImmediatePropagation()之间有什么区别?

  ask by Arjun translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...