最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

triggers - In a Javascript event, how to determine stopPropagation() has been called? - Stack Overflow

programmeradmin2浏览0评论

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

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

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

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

Share Improve this question edited Jan 8, 2021 at 15:33 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Aug 31, 2011 at 15:50 cc youngcc young 20.2k32 gold badges94 silver badges150 bronze badges 3
  • Step No. 1: w3.org/TR/DOM-Level-3-Events/#webidl-events-Event – Šime Vidas Commented Aug 31, 2011 at 15:57
  • so you're saying that if the event is in CAPTURING PHASE then stopPropagation() has been called? – cc young Commented Aug 31, 2011 at 16:04
  • @young No, that's unrelated... I'm not even sure that all browsers do implement capturing phases. – Šime Vidas Commented Aug 31, 2011 at 16:08
Add a comment  | 

3 Answers 3

Reset to default 10

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.

Something like:

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

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.)

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

isPropagationStopped: returnFalse

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

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

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论