Get value of current event handler using jQuery

后端 未结 2 1894
礼貌的吻别
礼貌的吻别 2021-01-18 15:54

I can set the onclick handler using jQuery by calling

$(\'#id\').click(function(){
   console.log(\'click!\');
});

Also using jQuery, how can

相关标签:
2条回答
  • 2021-01-18 16:16

    if you dont know the name of the function you can use

    args.callee

    https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments/callee

    function clickHandle(e){
      if($(e.target) == $('#id')) {
      $(newTarget).bind('click',  clickHandle);
      }
    }
    
    $('#id').bind('click',clickHandle);
    

    I think this would be the most symantic way of going about it

    0 讨论(0)
  • 2021-01-18 16:20

    jQuery's .click(function) method adds the function to a queue that is executed on the click event~

    So actually pulling out a reference to the given function would probably be hairy-er than you expect.

    As noted by others, it would be better to pass in a reference to the function; and then you already have the reference you need.

    var clicky = function () { /* do stuff */ };
    $('#id').click(clicky);
    // Do other stuff with clicky
    

    Update

    If you really really need to get it out, try this:

    jQuery._data(document.getElementById('id')).events.click[0].handler
    

    Depending on your version of jQuery that may or may not work~ Try playing around with

    jQuery._data(document.getElementById('id'))
    

    and see what you get.

    Got the idea from this section of the source:

    https://github.com/jquery/jquery/blob/master/src/event.js#LC36

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