【问题标题】:jQuery "unbind" back to "bind" not workingjQuery“取消绑定”回到“绑定”不起作用
【发布时间】:2013-01-30 19:14:36
【问题描述】:

当我单击一个元素时,我想取消绑定“mouseenter”和“mouseleave”事件,这可以正常工作,但如果单击另一个元素,我想将它们重新绑定 - 这不起作用。

有什么帮助吗?

代码如下:

<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>

非常感谢!

【问题讨论】:

  • 您需要在绑定中关联一个函数,例如。 bind('mouseenter mouseleave', function() { $(this).toggleClass('entered'); });
  • (a) 你为什么使用bind/unbind 而不是on/off? (b) 要附加一个事件处理程序,您需要指定一个!

标签: jquery bind unbind


【解决方案1】:

.bind() 函数希望您传递一个函数,以便在触发这些事件时执行。

$("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").bind('mouseenter mouseleave', function(event) {
    // do something here when the mouseenter or mouseleave events are triggered
});

当您调用.unbind() 时,事件处理程序被完全删除,jQuery 不会记住它是什么。你不能简单地调用.bind() 来撤消它并让它知道它应该执行什么代码来响应这些事件。

此外,根据您的 jQuery 版本(1.7+),您应该使用 .on().off() 函数来添加和删除事件处理程序。

【讨论】:

  • 嗯,基本上所有的#shapes 当鼠标进入时都会做不同的事情(动画、效果等)。我想禁用这些动画,当我点击某些东西时效果,所以它们保持静止,所以你不能鼠标进入它们,但是当你点击另一个元素时,我希望它们再次工作。
  • @Marcel 在这种情况下,您最好在这些函数中使用控制布尔值来决定是否执行动画等。当您单击时将其设置为 false其中一张图片,然后在您单击 #close 元素时将其设置回 true。
  • 或者,我假设您首先有添加这些事件处理程序的代码?您可以将其移至一个函数,并在需要重新绑定事件处理程序时简单地调用它。
  • 我将尝试使用 addClass 和 remove,这样他们就可以独立做事了..hmm..虽然不确定
【解决方案2】:

bind 只会为当前存在的项目绑定事件处理程序。

来自文档Bind()

处理程序附加到当前选定的元素 jQuery 对象,因此这些元素必须存在于调用点 .bind() 发生

使用 On 方法。

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

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

【讨论】:

  • .on() 的使用在功能上等同于他们当前对.bind() 的使用,它会做同样的事情。问题中没有任何迹象表明他正在使用动态添加的元素,但如果他这样做,那不是 .on() 处理它们的正确语法。
【解决方案3】:

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

试试:

<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>

【讨论】:

  • 我不想添加回调,因为当鼠标进入时每个形状都会做不同/独立的事情。
  • 你不能只是临时解除绑定事件。在所有情况下,您都需要重新分配要使用的回调。
  • 另一方面,您可以分配一个全局变量来定义是否必须不执行回调。首先检查你的回调是这样的: if(window.unboundEvents) return false;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多