简体   繁体   English

使父 div 不可点击,但儿童可点击 - CSS

[英]Make parent div unclickable but children children clickable - CSS

In a scenario like this one, I want to be able to click the background div (red) through the top div, whilst still being able to click the top div's children (blue and green).在这样的场景中,我希望能够通过顶部 div 单击背景 div(红色),同时仍然能够单击顶部 div 的子级(蓝色和绿色)。

 function bgclick() { console.log('Background Is Clicked!'); } function topclick() { console.log('Top Is Clicked!'); }
 #background { background-color: #f33; width: 250px; height: 250px; position: fixed; top: 0; left: 0; z-index: -1; } #top { width: 250px; height: 250px; position: fixed; top: 0; left: 0; } .children { width: 50px; height: 50px; position: relative; }
 <div id="background" onclick="bgclick()"></div> <div id="top"> <div class="children" onclick="topclick()" style="background-color:#3f3"></div> <div class="children" onclick="topclick()" style="background-color:#33f"></div> </div>

I have played around with pointer-events: none .我玩过指针事件: none This will only make one clickable and the other not.这只会使一个可点击而另一个不可点击。 How can I make it so I can click the red one and get a message, along with the blue and green ones?我怎样才能做到这样我就可以点击红色的并收到一条消息,以及蓝色和绿色的消息?

You have to change the markup by nesting the #top container into the #background container.您必须通过将#top容器嵌套到#background容器中来更改标记。

And then simply pass the event to the topclick method and add event.stopPropagation() to it to make sure just the div on the very top gets clicked.然后简单地将事件传递给topclick方法并将event.stopPropagation()添加到它以确保只点击最顶部的div

See the modified code:查看修改后的代码:

 function bgclick() { console.log('Background Is Clicked!') } function topclick(event) { event.stopPropagation(); console.log('Top Is Clicked!') }
 #background { background-color: #f33; width: 250px; height: 250px; position: fixed; top: 0; left: 0; z-index: -1; } #top { width: 250px; height: 250px; position: fixed; top: 0; left: 0; } .children { width: 50px; height: 50px; position: relative; }
 <div id="background" onclick="bgclick()"> <div id="top"> <div class="children" onclick="topclick(event)" style="background-color:#3f3"> </div> <div class="children" onclick="topclick(event)" style="background-color:#33f"> </div> </div> </div>

To make parent div unclickable but children clickable only use pointer-events.要使父 div 不可点击但子 div 可点击,仅使用指针事件。

pointer-events指针事件

The element is never the target of pointer events;元素永远不是指针事件的目标; however, pointer events may target its descendant elements if those descendants have pointer-events set to some other value.但是,如果这些后代将指针事件设置为其他值,则指针事件可能会针对其后代元素。 In these circumstances, pointer events will trigger event listeners on this parent element as appropriate on their way to/from the descendant during the event capture/bubble phases.在这些情况下,指针事件将在事件捕获/冒泡阶段期间,在它们往返后代的途中适当地触发此父元素上的事件侦听器。

More about how it works herepointer-events更多关于它是如何在这里工作的指针事件

.parent { 
    pointer-events: none;
}

.child {
    pointer-events: auto;
}

I've found a easy way to get around this problem (op).我找到了一种简单的方法来解决这个问题(op)。

pointer-events:none;指针事件:无; makes it so the mouse won't interact with the element.使鼠标不会与元素交互。 By default, pointer-events is inherited, so just set the children as pointer-events:auto;默认情况下,指针事件是继承的,因此只需将子项设置为指针事件:自动; . .

 function bgclick() { console.log('Background Is Clicked!'); } function topclick() { console.log('Top Is Clicked!'); }
 #background { background-color: #f33; width: 250px; height: 250px; position: fixed; top: 0; left: 0; z-index: -1; } #top { width: 250px; height: 250px; position: fixed; top: 0; left: 0; pointer-events: none; } .children { width: 50px; height: 50px; position: relative; pointer-events: auto; }
 <div id="background" onclick="bgclick()"></div> <div id="top"> <div class="children" onclick="topclick()" style="background-color:#3f3"></div> <div class="children" onclick="topclick()" style="background-color:#33f"></div> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM