简体   繁体   中英

Jquery get the actual control that fired the event.

I have a following html:-

<button type="button" class="btn-add">
  <i class="some-icon"></i>
</button>

which creates a nice fancy button with an icon

I bound an event on the button click:-

function (e) {
  console.log($(e.target));
}

now when i click on the center of the button the output of the above javascript is <i> element because of e.target. Is there any other way to get the actual control that fired this event, in this case the <button> ?

Read this Difference between target and currentTarget

Here is your solution: Fiddle link

$('.btn-add').click(function(e) {
    alert(e.currentTarget.tagName);
});

You can use either this or e.currentTarget

$('button').click(function(e){
    console.log(e.target);
    console.log(this);
    console.log(e.currentTarget);
})

Demo: Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM