代码之家  ›  专栏  ›  技术社区  ›  Mnebuerquo

如何在jquery中读取绑定的悬停回调函数

  •  3
  • Mnebuerquo  · 技术社区  · 16 年前

    我使用jQuery为页面上的元素设置悬停回调。我现在正在编写一个模块,它需要为一些元素临时设置新的悬停行为。新模块无法访问悬停函数的原始代码。

    我希望在设置新的悬停函数之前存储旧的悬停函数,以便在完成临时悬停行为后恢复它们。

    jQuery.data() 功能:

    //save old hover behavior (somehow)
    
    $('#foo').data('oldhoverin',???)
    
    $('#foo').data('oldhoverout',???);
    
    //set new hover behavior
    
    $('#foo').hover(newhoverin,newhoverout);
    

    做一些新的悬停行为。。。

    //restore old hover behaviour
    $('#foo').hover($('#foo').data('oldhoverin'),$('#foo').data('oldhoverout'));
    

    但是如何从jQuery获取当前注册的悬停函数呢?

    Shadow2531,我尝试在不修改最初注册回调的代码的情况下执行此操作。否则你的建议就行了。谢谢你的建议,也谢谢你帮我澄清我在寻找什么。也许我必须深入jquery的源代码,弄清楚这些回调是如何在内部存储的。也许我应该将问题改为“是否可以在不修改jquery的情况下执行此操作?”

    4 回复  |  直到 6 年前
        1
  •  4
  •   samjudson    16 年前

    召集活动 bind hover )不删除旧事件处理程序,只添加新事件,因此“还原”旧事件函数的想法不起作用,因为它不会删除事件。

    您可以添加自己的事件,然后在不影响任何其他事件的情况下删除它们,然后使用事件名称空间: http://docs.jquery.com/Events_(Guide)#Namespacing_events

        2
  •  1
  •   Shadow2531    16 年前

    
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <title>Jquery - Get, change and restore hover handlers</title>
            <script src="jquery.js"></script>
            <script>
                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);
                });
            </script>
        </head>
        <body>
            <p><a id="test" href="">test</a></p>
        </body>
    </html>
    

    如果没有,也许它会给你一些想法。

        3
  •  0
  •   user4903 user4903    16 年前

    http://docs.jquery.com/Events/bind

    因此,添加悬停事件,编写悬停所需的功能脚本,然后触发自定义事件。

        4
  •  0
  •   Erlend Halvorsen    16 年前

    也许隐藏旧元素并创建一个连接了事件处理程序的克隆会更容易一些?完成后,只需将旧元素调回。。