最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get value of current event handler using jQuery - Stack Overflow

programmeradmin0浏览0评论

I can set the onclick handler using jQuery by calling

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

Also using jQuery, how can I get a reference to the function which is currently handling the click() event?

The reason is that I have another object and want to set its click handler to the same one as #id.

Update

Thank you for all the suggestions. The problem is that I do not know which function is currently handling the clicks. Keeping track of it would add state to an already plicated template-editing system.

I can set the onclick handler using jQuery by calling

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

Also using jQuery, how can I get a reference to the function which is currently handling the click() event?

The reason is that I have another object and want to set its click handler to the same one as #id.

Update

Thank you for all the suggestions. The problem is that I do not know which function is currently handling the clicks. Keeping track of it would add state to an already plicated template-editing system.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 31, 2012 at 20:04 Krystian CybulskiKrystian Cybulski 11.1k14 gold badges71 silver badges99 bronze badges 3
  • Thank you for all the suggestions. The problem is that I do not know which function is currently handling the clicks. Keeping track of it would add state to an already plicated template-editing system. – Krystian Cybulski Commented Jan 31, 2012 at 20:10
  • 1 developer.mozilla/en/JavaScript/Reference/… – samccone Commented Jan 31, 2012 at 20:12
  • I have found a different way to solve my issue, eliminating the need for this. Thank you all for putting so much effort into this. – Krystian Cybulski Commented Jan 31, 2012 at 20:37
Add a ment  | 

2 Answers 2

Reset to default 5

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./jquery/jquery/blob/master/src/event.js#LC36

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

args.callee

https://developer.mozilla/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)

  1. 暂无评论