Jquery 'click' not firing when icon is on button

风流意气都作罢 提交于 2019-12-01 22:18:44

问题


I use Bootstrap and jQuery. I have three buttons, each with an icon in it. When I bind an click-event to a button, it works when I click on the button (on the edge), but not when I click on the middle of it, on the icon...

My code looks like this:

<div class="btn-group">
   <button class="btn btn_change_status" id="134" rel="tooltip" data-original-title="Tooltip">
      <i class="icon-eye-open"></i>
   </button>
   ... 
</div>

...

$('button.btn_change_status').on('click', function(e) {
   alert(e.target.id);
   return false;
});

How could I bind all elements wrapped by my btn_change_status to the click-event?


回答1:


You need a different syntax, as per the documentation:

$('body').on('click', 'button.btn_change_status', function(e) {
   alert(e.target.id);
   return false;
});



回答2:


Update your css with the following. I'm assuming you don't need mouse events on your icon.

.icon-eye-open {

    pointer-events:none;
}

MDN Information regarding pointer-events




回答3:


Use the following syntax for .on()

$(document).on('click','btn_change_status', function(e) {
   //write your code here
});


来源:https://stackoverflow.com/questions/17085257/jquery-click-not-firing-when-icon-is-on-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!