简体   繁体   English

使用 jQuery.click() 覆盖 MooTools 事件

[英]Overriding a MooTools event with jQuery.click()

I have a library function, that I cannot change, which uses MooTools to attach an event.我有一个无法更改的库函数,它使用 MooTools 附加事件。

$(document.body).addEvents({
    "mousedown:relay(a.className)": function (a, b) {
        if (!a.rightClick) {
            //do something
        }
        return false
});

I'm not too familiar with MooTools, but I've tried everything I can think of to override this with a jQuery.click() event.我对 MooTools 不太熟悉,但我已经尝试了所有我能想到的用jQuery.click()事件覆盖它的方法。

I've tried attaching a listener to .className , its parents (a few levels up), and even the whole body ;我试过将一个监听器附加到.className ,它的父级(几个级别),甚至整个body each time, I use function (e) { e.preventDefault() } in my click event.每次,我在我的click事件中使用function (e) { e.preventDefault() }

I've always assumed the preventDefault() would always prevent all default events associated with the newly-triggered event.我一直假设preventDefault()将始终阻止与新触发的事件关联的所有默认事件。

Can anyone enlighten me as to why my event override isn't working?谁能告诉我为什么我的事件覆盖不起作用?

// EDIT // 编辑

I'm not looking to use MooTools because we will probably port everything to jQuery (or at least not MooTools) in the future.我不打算使用 MooTools,因为我们将来可能会将所有内容都移植到 jQuery(或者至少不是 MooTools)。 I guess I'll have to, though, if there's no other way.不过,如果没有其他办法,我想我将不得不这样做。

// EDIT 2 // 编辑 2

I've tried jQuery.off() in several different ways, including $j('body').off('click mousedown','.className');我已经以几种不同的方式尝试过jQuery.off() ,包括$j('body').off('click mousedown','.className'); and it still fails to remove the MooTools event.它仍然无法删除 MooTools 事件。

// EDIT 3 // 编辑 3

I decided it was best to just not mess with MooTools;我决定最好不要乱用 MooTools; I've changed the classname so the MooTools doesn't do anything and just replicated what happened in the //do something part of the code above.我已经改变了类名,所以 MooTools 不做任何事情,只是复制上面代码的//do something部分发生的//do something

I have a feeling I can't do anything with the click event because of the return false at the end of the MooTools event listener.我有一种感觉,因为在 MooTools 事件侦听器结束时return false ,我无法对 click 事件做任何事情。

Depending on what version of JQuery you are using, you could try off (JQuery 1.7 or later) or unbind to remove the MooTools event handlers.根据您使用的 JQuery 版本,您可以尝试off (JQuery 1.7 或更高版本)或unbind以删除 MooTools 事件处理程序。

I am not sure how they work behind the scenes but I assume they will remove event handlers regardless of how they were bound the event.我不确定他们在幕后是如何工作的,但我认为无论他们如何绑定事件,他们都会删除事件处理程序。 You could then apply your own event handlers using JQuery.然后您可以使用 JQuery 应用您自己的事件处理程序。

http://api.jquery.com/off/ http://api.jquery.com/off/

http://api.jquery.com/unbind/ http://api.jquery.com/unbind/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM