在javascript事件中,如何确定stopPropagation()被调用了?

时间:2022-09-19 00:01:03

If e.preventDefault() is called, can see reflected in e.defaultPrevented method.

如果调用e.preventDefault(),则可以在. default阻止方法中看到反射。

Is there a similar property for e.stopPropagation()? If not, how to determine?

对于e.stopPropagation()是否有类似的属性?如果没有,如何确定?

3 个解决方案

#1


6  

I haven't looked through jQuery to see their method, but it seems you could override the stopPropagation method on the base Event object, set a flag, and then call the overridden method.

我还没有通过jQuery查看它们的方法,但是似乎可以覆盖基本事件对象上的stopPropagation方法,设置一个标志,然后调用重写的方法。

Something like:

喜欢的东西:

var overriddenStop =  Event.prototype.stopPropagation;
Event.prototype.stopPropagation = function(){
    this.isPropagationStopped = true;
    overriddenStop.apply(this, arguments);
}

#2


5  

No, there isn't.

不,没有。

I can tell, because jQuery's isPropagationStopped() method doesn't use any browser API internally, but instead implements this feature manually. (If there were such a feature in the browsers - built-in - jQuery would use it instead of doing it manually.)

我可以看出,因为jQuery的isPropagationStopped()方法在内部不使用任何浏览器API,而是手动实现这个特性。(如果浏览器中有这样的特性——内置的——jQuery就会使用它,而不是手工操作。)

So, in jQuery, a new event object will get this property (inherited):

因此,在jQuery中,一个新的事件对象将获得这个属性(继承):

isPropagationStopped: returnFalse

and then, when you invoke stopPropagation() on that event object, jQuery will manually alter this property:

然后,当您在该事件对象上调用stopPropagation()时,jQuery将手动修改此属性:

stopPropagation: function() {
    this.isPropagationStopped = returnTrue;
    ...
}

returnFalse and returnTrue are functions which return false and true respectively.

returnFalse和returnTrue分别是返回false和true的函数。

#3


0  

In IE, you can check the property e.cancelBubble to check for native stops. Other browsers do not have this functionality.

在IE中,可以检查属性e。取消气泡以检查本机停止。其他浏览器没有这个功能。

However, you could just keep track of it yourself. Because of the nature of it, you wouldn't be able to check it anywhere, except in the same handler that stops the propagation itself (the event won't bubble up, more handlers won't get called), so I can't imagine a situation where it would be necessary. What are you trying to do?

但是,你可以自己记录下来。由于它的性质,您无法在任何地方检查它,除非在相同的处理程序中停止传播本身(事件不会冒泡,更多的处理程序不会被调用),所以我无法想象有必要的情况。你想做什么?

Edit: I thought you were using jQuery at first. For those who are: You can use the e.isPropagationStopped() method, which will check if propagation has been stopped from jQuery. If propogation is stopped natively, this won't be able to tell.

编辑:一开始我还以为你在用jQuery呢。对于这样的人:您可以使用e.isPropagationStopped()方法,该方法将检查传播是否已从jQuery停止。如果原生代位停止,这将无法判断。

#1


6  

I haven't looked through jQuery to see their method, but it seems you could override the stopPropagation method on the base Event object, set a flag, and then call the overridden method.

我还没有通过jQuery查看它们的方法,但是似乎可以覆盖基本事件对象上的stopPropagation方法,设置一个标志,然后调用重写的方法。

Something like:

喜欢的东西:

var overriddenStop =  Event.prototype.stopPropagation;
Event.prototype.stopPropagation = function(){
    this.isPropagationStopped = true;
    overriddenStop.apply(this, arguments);
}

#2


5  

No, there isn't.

不,没有。

I can tell, because jQuery's isPropagationStopped() method doesn't use any browser API internally, but instead implements this feature manually. (If there were such a feature in the browsers - built-in - jQuery would use it instead of doing it manually.)

我可以看出,因为jQuery的isPropagationStopped()方法在内部不使用任何浏览器API,而是手动实现这个特性。(如果浏览器中有这样的特性——内置的——jQuery就会使用它,而不是手工操作。)

So, in jQuery, a new event object will get this property (inherited):

因此,在jQuery中,一个新的事件对象将获得这个属性(继承):

isPropagationStopped: returnFalse

and then, when you invoke stopPropagation() on that event object, jQuery will manually alter this property:

然后,当您在该事件对象上调用stopPropagation()时,jQuery将手动修改此属性:

stopPropagation: function() {
    this.isPropagationStopped = returnTrue;
    ...
}

returnFalse and returnTrue are functions which return false and true respectively.

returnFalse和returnTrue分别是返回false和true的函数。

#3


0  

In IE, you can check the property e.cancelBubble to check for native stops. Other browsers do not have this functionality.

在IE中,可以检查属性e。取消气泡以检查本机停止。其他浏览器没有这个功能。

However, you could just keep track of it yourself. Because of the nature of it, you wouldn't be able to check it anywhere, except in the same handler that stops the propagation itself (the event won't bubble up, more handlers won't get called), so I can't imagine a situation where it would be necessary. What are you trying to do?

但是,你可以自己记录下来。由于它的性质,您无法在任何地方检查它,除非在相同的处理程序中停止传播本身(事件不会冒泡,更多的处理程序不会被调用),所以我无法想象有必要的情况。你想做什么?

Edit: I thought you were using jQuery at first. For those who are: You can use the e.isPropagationStopped() method, which will check if propagation has been stopped from jQuery. If propogation is stopped natively, this won't be able to tell.

编辑:一开始我还以为你在用jQuery呢。对于这样的人:您可以使用e.isPropagationStopped()方法,该方法将检查传播是否已从jQuery停止。如果原生代位停止,这将无法判断。