简体   繁体   English

如何在父div上捕获事件?

[英]How to catch event on a parent div?

I would like to ask: 我想问一下:

  • How to make the group displays OVER the divs a and b? 如何使组显示在div a和b之上?
  • I want to catch event mousedown on group even if a user click on a div a or b. 即使用户单击div a或b,我也想在组上捕获事件mousedown。

Basically the group should visually cover the two other divs, so when I click inside the are covered by the group, only group div should detect mousedown. 基本上,该组应该在视觉上覆盖另外两个div,因此,当我在该组所覆盖的区域内单击时,只有div组才能检测到mousedown。

Notes: I cannot change HTML structure. 注意:我无法更改HTML结构。

 document.getElementById('group').addEventListener('click', function(event) { event.stopPropagation(); alert('click on: ' + event.target.id); }); 
 #group { position: absolute; width: 250px; height: 250px; background-color: yellow; z-index: 2; opacity: 0.5; } #a { position: absolute; left: 80px; width: 50px; height: 50px; margin: 10px; background-color: blue; z-index: 0; } #b { position: absolute; width: 50px; height: 50px; margin: 10px; background-color: blue; z-index: 0; } 
 <div id="group"> <div id="a">A</div> <div id="b">B</div> </div> 

  1. Use opacity 0 for the children. 为孩子们使用不透明度0。

  2. Use event.currentTarget instead of event.target . 使用event.currentTarget而不是event.target

With this config children are actually above groups , but are not visible and events are caught by groups elem, not children. 使用此配置,子级实际上位于groups之上,但不可见,并且事件由groups elem而非子级捕获。

 document.getElementById('group').addEventListener('click', function(event) { event.stopPropagation(); alert('click on: ' + event.currentTarget.id); }); 
 #group { position: relative; width: 250px; height: 250px; background-color: yellow; z-index: 2; opacity: 0.5; } #a { position: relative; left: 80px; width: 50px; height: 50px; margin: 10px; background-color: blue; z-index: 0; opacity: 0 } #b { position: relative; width: 50px; height: 50px; margin: 10px; background-color: blue; z-index: 0; opacity: 0; } 
 <div id="group"> <div id="a">A</div> <div id="b">B</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何捕获父元素中的模糊事件 - How to catch blur event in parent element 如何在不触发父级div事件的情况下触发子级div事件? - How to fire child div event without firing parent div event? 如何将mouseover事件发送到父div? - How to send mouseover event to parent div? 在 div 之外单击时如何捕获 ng-keypress/$event? - How to catch ng-keypress/ $event when clicked outside of div? React:如何在 `contentEditable` div 上使用 `InputEvent` 参数捕获 `onInput` 事件? - React: How to catch `onInput` event with `InputEvent` arguments on `contentEditable` div? 单击子div时如何取消绑定父div的click事件 - how to unbind the click event of parent div when clicked on child div 如何使子div上的onclick事件影响父div的透明度? - How to make an onclick event on child div affect the transparency of parent div? 如何仅在子 div 元素中而不在父 div 中触发 onclick 事件? - How to trigger onclick event only in the child div element but not in the parent div? 如何捕获此事件,当我左键单击然后在div上移动一些位置(而不是拖动div) - how to catch this event that when i left click and then move some where (not drag some div ) on a div 如何捕捉 stopPropagation 事件 - How to catch event that was stopPropagation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM