Two differents OnClick on two divs, one over the other

后端 未结 6 1567
心在旅途
心在旅途 2020-12-06 06:50

I have two divs, a big one and a smaller over the other, each div has its own OnClick method. The problem I have is when I clicked the smaller div, the big div\'s OnClick me

6条回答
  •  渐次进展
    2020-12-06 07:17

    Your problem is that the click event will propagate up the element tree. Therefore, each element that contains an element that was clicked will also fire a click event.

    The simplest solution is to add return false your handler.

    If you're using jQuery, you can call e.stopPropagation(); otherwise, you'll need to call e.stopPropagation() if it exists, and set event.cancelBubble = true.

    For more information, see here.

提交回复
热议问题