admin 管理员组文章数量: 1086019
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 behaviour 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 behaviour.
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 behaviour...
//restore old hover behaviour
$('#foo').hover($('#foo').data('oldhoverin'),$('#foo').data('oldhoverout'));
But how do I get the currently registered hover functions from 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?"
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 behaviour 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 behaviour.
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 behaviour...
//restore old hover behaviour
$('#foo').hover($('#foo').data('oldhoverin'),$('#foo').data('oldhoverout'));
But how do I get the currently registered hover functions from 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?"
Share Improve this question edited Mar 22, 2019 at 15:16 Shiladitya 12.2k17 gold badges28 silver badges42 bronze badges asked Sep 8, 2008 at 0:29 MnebuerquoMnebuerquo 5,9796 gold badges48 silver badges54 bronze badges4 Answers
Reset to default 4Calling 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.
You can add your own events, and then remove them without affecting any other events then use Event namespacing: http://docs.jquery./Events_(Guide)#Namespacing_events
Not sure if this will work, but you can try this:
function setHover(obj, mouseenter, mouseleave) {
obj.data("_mouseenter", mouseenter);
obj.data("_mouseleave", mouseleave);
obj.hover(obj.data("_mouseenter"), obj.data("_mouseleave"));
}
function removeHover(obj) {
obj.unbind("mouseenter", obj.data("_mouseenter"));
obj.unbind("mouseleave", obj.data("_mouseleave"));
obj.data("_mouseenter", undefined);
obj.data("_mouseleave", undefined);
}
$(document).ready(function() {
var test = $("#test");
setHover(test, function(e) {
alert("original " + e.type);
}, function(e) {
alert("original " + e.type);
});
var saved_mouseenter = test.data("_mouseenter");
var saved_mouseleave = test.data("_mouseleave");
removeHover(test);
setHover(test, function() {
alert("zip");
}, function() {
alert('zam');
});
removeHover(test);
setHover(test, saved_mouseenter, saved_mouseleave);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Jquery - Get, change and restore hover handlers</title>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<p><a id="test" href="">test</a></p>
</body>
</html>
If not, maybe it'll give you some ideas.
I'm not sure if this is what you mean, but you can bind custom events and then trigger them.
http://docs.jquery./Events/bind
So add your hover event, script the functionality you need for that hover, then trigger your custom event.
Maybe it would be easier to just hide the old element and create a clone with your event handlers attached? Then just swap back in the old element when you're done..
本文标签: javascriptHow to read bound hover callback functions in jQueryStack Overflow
版权声明:本文标题:javascript - How to read bound hover callback functions in jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1743985583a2513797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论