如何在jQuery中撤消event.stopPropagation?

我在我的应用程序中使用event.stopPropagation() 。 然而,出现了一种情况,我希望继续传播,就像从未调用上述函数一样。 所以我想知道; 有没有办法在它停止后“恢复”传播 ? 将一次调用event.stopPropagation移动到十几个单独的条件语句是非常繁琐的。

一旦传播停止,就无法恢复。 作为一种解决方法,您可以做的是设置变量,然后仅在此变量为真时停止:

 var stop = false; // do your logic here if(stop){ event.stopPropagation(); } 

event.stopPropagation()调用放在您的条件中。 例如

 $el.click(function(event) { if (some_condition) { event.stopPropagation() // do stuff } else { // do other stuff, without stopping propagation } }); 

它可能很繁琐,但不幸的是stopPropagation是单向转换。 停止后,您无法为同一事件重新打开它。

只需像这样重构原始事件:

 var refEvent = event.originalEvent; refEvent.cancelBubble = false; refEvent.defaultPrevented = false; refEvent.returnValue = true; refEvent.timeStamp = (new Date()).getTime(); if (event.target.dispatchEvent){ event.target.dispatchEvent(refEvent); } else if (event.target.fireEvent) { event.target.fireEvent(refEvent); } 

这是一个有效的解决方案:

 $('#mydiv').on('click.new', function(e){ e.stopImmediatePropagation(); alert('this will happen only once'); $('#mydiv').unbind('click.new'); }); 

它利用了事件可以具有自定义命名空间的事实,并且可以相应地解除绑定。 它也适用于e.stopPropagation 。 就此而言,它可以撤消与该自定义点击事件相关的任何内容。