Click events on overlapping items

情到浓时终转凉″ 提交于 2019-12-10 03:28:48

问题


I have

  • table row with click event
  • button with click event, that button is on table row

and I have problem. When I hit button, row click event execute too, but I don't want this behavior. I want only button click execute, without row click.


回答1:


look at your code this is what you should do in the button click event stopPropagation




回答2:


Using jQuery (due to question tag):

$('#yourButton').click(function(e) {
    // stop event from bubbling up to row element
    e.stopPropagation();

    // now do your stuff
});



回答3:


You can call an extra function onClick

this is the function:

function cancelBubble(e){

    e.cancelBubble = true;
    if(e.stopPropagation)
     e.stopPropagation();
}

And button's onClick you can write like

onclick="yourfunctionname();cancelBubble(event)"

yourfunctionname:: is the name of your function you are already calling.




回答4:


Also I had a dropdown overlayed on a clickable element. So just use the following to prevent propagation to the underlying element for all modern browsers

onclick="yourfunctiontohandle();event.stopPropagation();"

For older cross browser solution, must use IE event.cancelBubble

onclick="if(event.stopPropagation) event.stopPropagation(); else event.cancelBubble=true;"


来源:https://stackoverflow.com/questions/1728867/click-events-on-overlapping-items

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