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

$(".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事件吗?
阅读 2.6k
1 个回答

谢邀.

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

以上.

推荐问题