jQuery: Can I get a reference to the bound events on an element?

前端 未结 2 1503
走了就别回头了
走了就别回头了 2021-02-06 05:26

I have some elements with a function bound to the click event. I want to bind that same function instead to the mouseover and mouseout eve

2条回答
  •  醉梦人生
    2021-02-06 05:45

    In jQuery, all the events bound by jQuery are stored in data under the key events. The following would do what you want:

    var $this = $(this),
        events = $this.data('events');
    if( events && events['click'] ){
      // Loop through each click event bound to this control
      $.each( events['click'], function(){
       // this = the function
       $this.bind('mouseover mouseout', this);
      });
      // Finally, remove all `click` handlers with one call
      $this.unbind('click');
    }
    

提交回复
热议问题