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

发布于 2024-12-02 02:46:37 字数 147 浏览 0 评论 0原文

如果调用e.preventDefault(),可以在e.defaultPrevented方法中看到反映。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泡沫很甜 2024-12-09 02:46:37

我没有通过 jQuery 查看他们的方法,但似乎您可以重写基本 Event 对象上的 stopPropagation 方法,设置一个标志,然后调用重写的方法方法。

像这样的东西:

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

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);
}
不打扰别人 2024-12-09 02:46:37

不,没有。

我可以这么说,因为 jQuery 的 isPropagationStopped() 方法在内部没有使用任何浏览器 API,而是手动实现此功能。 (如果浏览器中有这样的功能 - 内置 - jQuery 将使用它而不是手动执行。)

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

isPropagationStopped: returnFalse

然后,当您调用 <在该事件对象上的 code>stopPropagation() 上,jQuery 将手动更改此属性:

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

returnFalsereturnTrue 是返回 false 的函数和分别为true

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.

情话难免假 2024-12-09 02:46:37

在 IE 中,您可以检查属性 e.cancelBubble 来检查本机停止。其他浏览器没有此功能。

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

编辑:我以为你一开始使用的是 jQuery。对于那些:
您可以使用 e.isPropagationStopped() 方法,该方法将检查 jQuery 是否已停止传播。如果传播本身就停止了,这将无法判断。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文