How to debug JavaScript / jQuery event bindings with Firebug or similar tools?

前端 未结 15 1353
一个人的身影
一个人的身影 2020-11-22 06:56

I need to debug a web application that uses jQuery to do some fairly complex and messy DOM manipulation. At one point, some of the events that were bound to particular eleme

15条回答
  •  青春惊慌失措
    2020-11-22 07:24

    See How to find event listeners on a DOM node.

    In a nutshell, assuming at some point an event handler is attached to your element (eg): $('#foo').click(function() { console.log('clicked!') });

    You inspect it like so:

    • jQuery 1.3.x

      var clickEvents = $('#foo').data("events").click;
      jQuery.each(clickEvents, function(key, value) {
        console.log(value) // prints "function() { console.log('clicked!') }"
      })
      
    • jQuery 1.4.x

      var clickEvents = $('#foo').data("events").click;
      jQuery.each(clickEvents, function(key, handlerObj) {
        console.log(handlerObj.handler) // prints "function() { console.log('clicked!') }"
      })
      

    See jQuery.fn.data (where jQuery stores your handler internally).

    • jQuery 1.8.x

      var clickEvents = $._data($('#foo')[0], "events").click;
      jQuery.each(clickEvents, function(key, handlerObj) {
        console.log(handlerObj.handler) // prints "function() { console.log('clicked!') }"
      })
      

提交回复
热议问题