简体   繁体   中英

Clickable area around overlay content

It's probably an easy solution but I just can't get my head around it. What I want to do is to make an overlay layer with child layers clickable, without assigning any interaction to the children. Like so -

<div class="overlay">
    <div class="content">Lorem ipsum dolor.</div>
</div>

jQuery sample function:

$('.overlay').click(function() {
    $(this).hide();
});

What happens here is that the function runs even if I click on a child layer. How should I set this up so that only the area around the children is affected?

You could check the click target by e.target

$('.overlay').click(function(e) {
    if (!$(e.target).is('.content')) {
        $(this).hide();
    }
});

try with e.stopPropagation(); -

$('.content').click(function(e) {
    e.stopPropagation();
    alert('child');
});

JSFiddle

创建的示例,单击内容可防止隐藏操作示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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