How to find event listeners on a DOM node when debugging or from the JavaScript code?

前端 未结 19 2566
臣服心动
臣服心动 2020-11-21 05:34

I have a page where some event listeners are attached to input boxes and select boxes. Is there a way to find out which event listeners are observing a particular DOM node a

19条回答
  •  青春惊慌失措
    2020-11-21 06:02

    I am trying to do that in jQuery 2.1, and with the "$().click() -> $(element).data("events").click;" method it doesn't work.

    I realized that only the $._data() functions works in my case :

    	$(document).ready(function(){
    
    		var node = $('body');
    		
            // Bind 3 events to body click
    		node.click(function(e) { alert('hello');  })
    			.click(function(e) { alert('bye');  })
    			.click(fun_1);
    
            // Inspect the events of body
    		var events = $._data(node[0], "events").click;
    		var ev1 = events[0].handler // -> function(e) { alert('hello')
    		var ev2 = events[1].handler // -> function(e) { alert('bye')
    		var ev3 = events[2].handler // -> function fun_1()
            
    		$('body')
    			.append('

    Event1 = ' + eval(ev1).toString() + '

    ') .append('

    Event2 = ' + eval(ev2).toString() + '

    ') .append('

    Event3 = ' + eval(ev3).toString() + '

    '); }); function fun_1() { var txt = 'text del missatge'; alert(txt); }
    
    
    
    

提交回复
热议问题