仅当默认事件被阻止/未被阻止时才运行函数

发布于 2024-10-04 13:00:39 字数 769 浏览 0 评论 0原文

有没有办法仅在事件(由另一个未知函数)调用 event.preventDefault() 时运行函数。这是一个 jQuery 插件,所以我不知道页面的其他部分可能在做什么。我已经尝试过这个:

Event.test = Event.preventDefault;
Event.preventDefault = function () {
    alert('Success');
    this.test();
}

但它不起作用......只是表现正常,没有错误。

相反,我也希望相反......仅在未调用 event.preventDefault() 时才调用函数。实际上,将函数添加到事件的默认操作中。有什么想法吗?这一切有可能吗?

编辑:根据评论,我找到了第一个问题的解决方案: http://jsfiddle .net/nathan/VAePB/9/。它在 Chrome 中工作(警报 function PreventDefault() { [native code] },但 IE 警报 undefined。所以 IE 不会让我定义 Event.prototype .test,但它会让我重新定义Event.prototype.preventDefault,我确信如果我可以根据这个问题找到第二个问题的解决方案。可以让它在 IE 中工作。

Is there a way to run a function only if event.preventDefault() is called on an event (by another unknown function). This is for a jQuery plugin, so I don't have any knowledge of what other parts of the page might be doing. I've tried this:

Event.test = Event.preventDefault;
Event.preventDefault = function () {
    alert('Success');
    this.test();
}

but it doesn't work... just behaves as normal, with no errors.

Conversely, I want the opposite too... to call a function only if event.preventDefault() isn't called. In effect, to add a function to the default action for an event. Any ideas? Is all this at all possible?

Edit: Based on the comment, I've got a solution to the first problem: http://jsfiddle.net/nathan/VAePB/9/. It works in Chrome (alerts function preventDefault() { [native code] }, but IE alerts undefined. So IE won't let me define Event.prototype.test, but it will let me redefine Event.prototype.preventDefault. Weird. I'm sure I can come up with a solution to the the second problem based on this one if I can just get it to work in IE.

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

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

发布评论

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

评论(2

东京女 2024-10-11 13:00:40

我不确定我是否理解了。你不能像这个那样使用event.isDefaultPrevented()

I'm not sure I've understand. Can't you just use event.isDefaultPrevented() like this

小鸟爱天空丶 2024-10-11 13:00:40

对于第一个问题,尝试这样的方法:

oldPreventDefault = Event.prototype.preventDefault;
Event.prototype.preventDefault = function() {
    //do stuff
    oldPreventDefault.call(this);
}

我不知道这是否有效,但可能值得一试。

对于第二个问题,我会尝试类似于实时事件处理的方法。将侦听器放在父元素上(即 body 或顶级 div)。如果您可以如前所述将钩子插入到 preventDefault 中,则可以使用它来设置标志。如果事件冒泡到该元素并且您的标志未设置,请执行您的扩展行为。尽管这不适用于所有事件,因为并非所有事件都会冒泡。解决此问题的另一种方法可能是使用 setTimeout(0,...) 延迟执行,直到当前堆栈完成,然后检查标志。

For the first problem, try something like this:

oldPreventDefault = Event.prototype.preventDefault;
Event.prototype.preventDefault = function() {
    //do stuff
    oldPreventDefault.call(this);
}

I don't know if that will work, but it might be worth a shot.

For the second problem, I would try something similar to live event handling. Put a listener on a parent element (i.e. body or a top-level div). If you can get your hook into preventDefault as noted before, you can use that to set a flag. If the event bubbles up to that element and your flag isn't set, do your extended behavior. Though this won't work with all events, since not all events bubble. Another way to tackle this problem might be to delay execution until the current stack has finished using setTimeout(0,...) and then checking the flag.

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