Can I find events bound on an element with jQuery?

前端 未结 9 2022
南旧
南旧 2020-11-22 07:11

I bind two event handlers on this link:

Show Alert

JavaScript:

$(         


        
9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 07:47

    In modern versions of jQuery, you would use the $._data method to find any events attached by jQuery to the element in question. Note, this is an internal-use only method:

    // Bind up a couple of event handlers
    $("#foo").on({
        click: function(){ alert("Hello") },
        mouseout: function(){ alert("World") }
    });
    
    // Lookup events for this particular Element
    $._data( $("#foo")[0], "events" );
    

    The result from $._data will be an object that contains both of the events we set (pictured below with the mouseout property expanded):

    Console output for $._

    Then in Chrome, you may right click the handler function and click "view function definition" to show you the exact spot where it is defined in your code.

提交回复
热议问题