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

前端 未结 15 1103
一个人的身影
一个人的身影 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:26

    Using DevTools in the latest Chrome (v29) I find these two tips very helpful for debugging events:

    1. Listing jQuery events of the last selected DOM element

      • Inspect an element on the page
      • type the following in the console:

        $._data($0, "events") //assuming jQuery 1.7+

      • It will list all jQuery event objects associated with it, expand the interested event, right-click on the function of the "handler" property and choose "Show function definition". It will open the file containing the specified function.

    2. Utilizing the monitorEvents() command

    0 讨论(0)
  • 2020-11-22 07:32

    Use $._data(htmlElement, "events") in jquery 1.7+;

    ex:

    $._data(document, "events") or $._data($('.class_name').get(0), "events")

    0 讨论(0)
  • 2020-11-22 07:32

    As a colleague suggested, console.log > alert:

    var clickEvents = $('#foo').data("events").click;
    jQuery.each(clickEvents, function(key, value) {
        console.log(value);
    })
    
    0 讨论(0)
  • 2020-11-22 07:34

    Looks like FireBug crew is working on an EventBug extension. It will add another panel to FireBug - Events.

    "The events panel will list all of the event handlers on the page grouped by event type. For each event type you can open up to see the elements the listeners are bound to and summary of the function source." EventBug Rising

    Although they cannot say right now when it will be released.

    0 讨论(0)
  • 2020-11-22 07:37

    Here's a plugin which can list all event handlers for any given element/event:

    $.fn.listHandlers = function(events, outputFunction) {
        return this.each(function(i){
            var elem = this,
                dEvents = $(this).data('events');
            if (!dEvents) {return;}
            $.each(dEvents, function(name, handler){
                if((new RegExp('^(' + (events === '*' ? '.+' : events.replace(',','|').replace(/^on/i,'')) + ')$' ,'i')).test(name)) {
                   $.each(handler, function(i,handler){
                       outputFunction(elem, '\n' + i + ': [' + name + '] : ' + handler );
                   });
               }
            });
        });
    };
    

    Use it like this:

    // List all onclick handlers of all anchor elements:
    $('a').listHandlers('onclick', console.info);
    
    // List all handlers for all events of all elements:
    $('*').listHandlers('*', console.info);
    
    // Write a custom output function:
    $('#whatever').listHandlers('click',function(element,data){
        $('body').prepend('<br />' + element.nodeName + ': <br /><pre>' + data + '<\/pre>');
    });
    

    Src: (my blog) -> http://james.padolsey.com/javascript/debug-jquery-events-with-listhandlers/

    0 讨论(0)
  • 2020-11-22 07:38

    Firebug 2 does now incorporate DOM events debugging / inspection.

    0 讨论(0)
提交回复
热议问题