jQuery“解除绑定"回到“绑定"状态不工作 [英] jQuery "unbind" back to "bind" not working

查看:124
本文介绍了jQuery“解除绑定"回到“绑定"状态不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击一个元素时,我想取消绑定"mouseenter"和"mouseleave"事件的正常工作,但是如果单击另一个元素,我想将它们重新绑定-这是行不通的.

When I click an element I would like to unbind "mouseenter" and "mouseleave" events which works fine, but I would like to bind them back on if another element is clicked - this does not work.

有什么帮助吗?

这是代码:

<script type="text/javascript">
  $(document).ready(function(){
        $("#shape1 img").click(function(){
          $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").unbind('mouseenter mouseleave');
    });

     $("#close").click(function(){
        $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").bind('mouseenter mouseleave');
     });
 });
</script>

非常感谢!

推荐答案

因为您需要分配回调以在事件发生时执行.

Because you need to assign the callbacks to execute when events occur.

尝试:

<script type="text/javascript">
  $(document).ready(function(){
        var myFunctionMouseEnter = function(){
           alert('Hey');
        };
        var myFunctionMouseleave = function(){
           alert('Hey');
        };

        $("#shape1 img").click(function(){
          $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").off('mouseenter mouseleave');
        });

        $("#close").click(function(){
            $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").on('mouseenter',myFunctionMouseEnter )
                                                                               .on('mouseleave',myFunctionMouseleave );
        });
 });
</script>

这篇关于jQuery“解除绑定"回到“绑定"状态不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆