2014-03-01 61 views
5

我在JavaScript中编写事件调度程序,我决定基于标准的js CustomEvent类。如果事件传播在侦听器中停止(通过e.stopPropagation()),我无法找到如何检测。我应该自己写一个Event对象实现吗?如何检查事件传播是否已停止?

+0

不知道你的意思。如果传播停止,您的听众将永远不会被调用。 – Bergi

+0

@Bergi这是很好的知道测试。 – mightyiam

回答

3

据我所知,没有办法,只能窥探Event.prototype.stopPropagation功能。

-2

你试过了吗?

e.isPropagationStopped() 

或者

event.isPropagationStopped() 
+0

这是一个jQuery特有的功能吗?我无法在MDN中找到任何信息。 – Olgierd

+2

不,它是一个Javascript事件。查看[W3C](http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/events.html#Events-Event-isPropagationStopped)。 – Koen

+0

这只适用于自定义事件,不适用于常规事件。 – James

0

如果将属性cancelable设置为true并且使用事件的preventDefault()方法,则CustomEvent将具有布尔属性defaultPrevented

var evt = new CustomEvent("custom", {cancelable : true}); 
console.log(evt.defaultPrevented); 
evt.preventDefault(); 
console.log(evt.defaultPrevented); 

Browser Support:铬18,IE 9,边缘,歌剧11,Safari浏览器5