简体   繁体   English

Jquery获取触发事件的实际控件。

[英]Jquery get the actual control that fired the event.

I have a following html:- 我有以下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. 现在,当我点击按钮的中心时,由于e.target,上面javascript的输出是<i>元素。 Is there any other way to get the actual control that fired this event, in this case the <button> ? 有没有其他方法可以获得触发此事件的实际控件,在这种情况下是<button>

Read this Difference between target and currentTarget 阅读target和currentTarget之间的这个区别

Here is your solution: Fiddle link 这是你的解决方案: 小提琴链接

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

You can use either this or e.currentTarget 您可以使用thise.currentTarget

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

Demo: Fiddle 演示: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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