如何读取jquery中的绑定悬停回调函数 [英] How to read bound hover callback functions in jquery
问题描述
我使用jquery为页面上的元素设置悬停回调。我现在正在写一个模块,需要暂时为某些元素设置新的悬停行为。新模块无法访问悬停函数的原始代码。
I used jquery to set hover callbacks for elements on my page. I'm now writing a module which needs to temporarily set new hover behavior for some elements. The new module has no access to the original code for the hover functions.
我想在设置新的悬停函数之前存储旧悬停函数,以便在完成后可以恢复它们
I want to store the old hover functions before I set new ones so I can restore them when finished with the temporary hover behavior.
我认为这些可以使用 jquery.data()
函数存储:/ p>
I think these can be stored using the jquery.data()
function:
//save old hover behavior (somehow)
$('#foo').data('oldhoverin',???)
$('#foo').data('oldhoverout',???);
//set new hover behavior
$('#foo').hover(newhoverin,newhoverout);
做新的悬停行为的东西...
Do stuff with new hover behavior...
//restore old hover behavior
$('#foo').hover($('#foo').data('oldhoverin'),$('#foo').data('oldhoverout'));
但是如何从jquery获取当前注册的悬停函数?
But how do I get the currently registered hover functions from jquery?
Shadow2531,我试图这样做而不修改最初注册回调的代码。你的建议会工作得很好,否则。感谢您的建议,并帮助澄清我正在寻找什么。也许我必须进入jquery的源,并找出这些回调如何存储在内部。也许我应该把问题改成是否可以不修改jquery就可以这么做?
Shadow2531, I am trying to do this without modifying the code which originally registered the callbacks. Your suggestion would work fine otherwise. Thanks for the suggestion, and for helping clarify what I'm searching for. Maybe I have to go into the source of jquery and figure out how these callbacks are stored internally. Maybe I should change the question to "Is it possible to do this without modifying jquery?"
推荐答案
调用事件 bind
方法(如 hover
)不会删除旧的事件处理程序,只会添加您的新事件,旧的事件函数不会工作,因为它不会删除您的事件。
Calling an event bind
method (such as hover
) does not delete old event handlers, only adds your new events, so your idea of 'restoring' the old event functions wouldn't work, as it wouldn't delete your events.
您可以添加自己的事件,然后删除它们,而不影响任何其他事件,然后使用事件命名空间: http://docs.jquery.com/Events_(指南)#Namespacing_events
You can add your own events, and then remove them without affecting any other events then use Event namespacing: http://docs.jquery.com/Events_(Guide)#Namespacing_events
这篇关于如何读取jquery中的绑定悬停回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!