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

前端 未结 2 1497
走了就别回头了
走了就别回头了 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');
    }
    
    0 讨论(0)
  • 2021-02-06 05:56

    Try this:

    jQuery('#element').data('events');
    

    You can also do this:

    jQuery.each(jQuery('#element').data('events'), function(i, event){
        jQuery.each(event, function(i, eventHandler){
            console.log("The handler is " + eventHandler.toString() );
        });
    });
    
    0 讨论(0)
提交回复
热议问题