admin管理员组

文章数量:1334342

I simply need to define a click event for this HTML:

<button data-method="rotate" class="btn btn-default">
    <i class="fa fa-rotate-left"></i>
</button>

html

Template.page.events({
    'click button': function (event) { 
        var $button     = $(event.target),
            method      = $button.attr('data-method');
    }
});

But now there is the problem, that this only works if the user doesn't click on the fa icon, because then the event target is different. In this case method will be undefined. How can I prevent this?

I simply need to define a click event for this HTML:

<button data-method="rotate" class="btn btn-default">
    <i class="fa fa-rotate-left"></i>
</button>

html

Template.page.events({
    'click button': function (event) { 
        var $button     = $(event.target),
            method      = $button.attr('data-method');
    }
});

But now there is the problem, that this only works if the user doesn't click on the fa icon, because then the event target is different. In this case method will be undefined. How can I prevent this?

Share Improve this question asked Nov 24, 2015 at 20:38 user3142695user3142695 17.4k55 gold badges196 silver badges375 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Why not just

$(this)   // or $(e.currentTarget)

instead of

$(event.target)

event.target points you to the actual element that triggers your event. But in your case you want that to be a button and all its descendent elements.

本文标签: javascriptJS event target for button with iconStack Overflow