为了账号安全,请及时绑定邮箱和手机立即绑定

hover函数解绑之后怎么重新再次绑定

hover函数解绑之后怎么重新再次绑定

繁华开满天机 2018-10-11 14:10:39
$(".hover").hover(function(){            $(this).css("background","#008000");      },function(){            $(this).css("background","#fff");      });                  $(".bind").click(function(){            alert("bind");            $('.hover').bind("hover");      });            $(".unbind").click(function(){            console.log("unbind succ===");            $('.hover').unbind('mouseenter').unbind('mouseleave');      });重新绑定,并没有成功,也就是  $('.hover').bind("hover");这个话没有执行。那有什么方法重新绑定hover事件吗?
查看完整描述

1 回答

?
HUX布斯

TA贡献1876条经验 获得超6个赞

  1. 事件绑定起码得有俩参数,分别是事件名和处理function。建议再去看看教程或者文档;

  2. 这个API有点老了(没记错的话jQ3已经彻底甩掉.bind()了,.toggle()在1.9就已经淘汰了),建议用新版API,.on().off()等;

  3. 修改(不知道绑的hover是干嘛用的,权且移花接木):

    function mEnter () {
        $(this).css("background","#008000");
    }function mLeave () {
        $(this).css("background","#fff");
    }
    $(".bind").click(function(){    console.log('bind!');
        $('.hover')
          .on('mouseenter', mEnter)
          .on('mouseleave', mLeave);
    });
    $(".unbind").click(function(){    console.log('unbind!');
        $('.hover')
          .off('mouseenter', mEnter)
          .off('mouseleave', mLeave);
    });
  4. 参考:jQuery:绑定事件处理器.on()


查看完整回答
反对 回复 2018-11-17
  • 1 回答
  • 0 关注
  • 675 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信