javascript - Prevent underlying div onclick event to trigger - Stack Overflow

I'm trying to create an overlay which has an outer div (half see-through) and above an smaller inn

I'm trying to create an overlay which has an outer div (half see-through) and above an smaller inner content div. Clicking on the outer area makes the overlay dissappear. Clicking on the content area should interact with the overlay content.

Looking at my example code, clicking on the red area first raises an alert "red" and afterwards an alert "black" thus closing the overlay immediatly after the first interaction.

How can I prevent the onclick event of the underlying black div to trigger when the above red div is clicked?

    <div onclick = "window.alert('black')" style = "background-color:black; width:100%; height:100%">
    	<div  onclick = "window.alert('red')" style = "background-color:red; position: absolute; top:10%; left:10%; width: 80%; height: 80%;">
    		some content
    	</div>
    </div>

I'm trying to create an overlay which has an outer div (half see-through) and above an smaller inner content div. Clicking on the outer area makes the overlay dissappear. Clicking on the content area should interact with the overlay content.

Looking at my example code, clicking on the red area first raises an alert "red" and afterwards an alert "black" thus closing the overlay immediatly after the first interaction.

How can I prevent the onclick event of the underlying black div to trigger when the above red div is clicked?

    <div onclick = "window.alert('black')" style = "background-color:black; width:100%; height:100%">
    	<div  onclick = "window.alert('red')" style = "background-color:red; position: absolute; top:10%; left:10%; width: 80%; height: 80%;">
    		some content
    	</div>
    </div>

I couldn't find anything online about that except people setting pointer-events to none which would disable the user to interact with the overlay content. Also setting different z-indeices didn't work either.

If you want to verify the clickthrough happening: https://onlinegdb./rJbOmB0FV

Share Improve this question edited Apr 12, 2019 at 18:13 fedesc 2,6103 gold badges25 silver badges39 bronze badges asked Apr 12, 2019 at 17:16 DamesDames 7764 silver badges12 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

This is more of a javascript behaviour you're experiencing.

Bubbling

The bubbling principle is simple.

When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.

Use event.stopPropagation()

Definition and Usage

  • The stopPropagation() method prevents propagation of the same event from being called.

  • Propagation means bubbling up to parent elements or capturing down to child elements.

https://www.w3schools./jsref/event_stoppropagation.asp

Update

A simple function will be easier to handle.

Something like this:

const alert = (text) => {
  event.stopPropagation();
  window.alert(text)
}
<div onclick = "alert('black')" style = "background-color:black; width:100%; height:100%">
    <div  onclick = "alert('red')" style = "background-color:red; position: absolute; top:10%; left:10%; width: 80%; height: 80%;">
        some content
    </div>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745335463a4623081.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信